/*

 * Title: japlot.jaxodraw
 * Description: Graphical user interface for drawing Feynman diagrams
 * @author Daniele Binosi 
 * @author Lukas Theussl  

 Copyright (C) 2003-2006,  Daniele Binosi and Lukas Theussl

 See the file LICENSE in the source distribution home directory
 for a full copy of the GPL (GNU General Public License).

    This file is part of japlot.jaxodraw.

    japlot.jaxodraw is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    japlot.jaxodraw is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 */
package jhplot.jadraw;


import japlot.jaxodraw.JaxoColor;
import japlot.jaxodraw.JaxoVertexT5OptionsPanel;

import java.awt.BasicStroke;
import java.awt.Dimension;
import org.freehep.graphics2d.VectorGraphics;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;



/** A vertex (a triangle). */
public class JaVertexT5 extends JaFillObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;

    /** Constructor: sets the width, height, relative width and height to a
     *  default value of 5 .
     */
    public JaVertexT5() {
        setRelw(5);
        setRelh(5);
    }

    /** Sets the x coordinate of the first click point of this vertex.
     * @param newx1 The x coordinate of the first click point of this vertex.
     */
    public final void setX1(double newx1) {
        this.x1 = newx1;
    }

    /** Returns the x coordinate of the first click point of this vertex.
    * @return The x coordinate of the first click point of this vertex.
    */
    public final double getX1() {
        return x1;
    }

    /** Sets the x coordinate of the second click point of this vertex.
     * @param newx2 The x coordinate of the second click point of this vertex.
     */
    public final void setX2(double newx2) {
        this.x2 = newx2;
    }

    /** Returns the x coordinate of the second click point of this vertex.
     * @return The x coordinate of the second click point of this vertex.
     */
    public final double getX2() {
        return x2;
    }

    /** Sets the x coordinate of the third click point of this vertex.
     * @param newx3 The x coordinate of the third click point of this vertex.
     */
    public final void setX3(double newx3) {
        this.x3 = newx3;
    }

    /** Returns the x coordinate of the third click point of this vertex.
     * @return The x coordinate of the third click point of this vertex.
     */
    public final double getX3() {
        return x3;
    }

    /** Sets the y coordinate of the first click point of this vertex.
     * @param newy1 The y coordinate of the first click point of this vertex.
     */
    public final void setY1(double newy1) {
        this.y1 = newy1;
    }

    /** Returns the y coordinate of the first click point of this vertex.
    * @return The y coordinate of the first click point of this vertex.
    */
    public final double getY1() {
        return y1;
    }

    /** Sets the y coordinate of the second click point of this vertex.
     * @param newy2 The y coordinate of the second click point of this vertex.
     */
    public final void setY2(double newy2) {
        this.y2 = newy2;
    }

    /** Returns the y coordinate of the second click point of this vertex.
     * @return The y coordinate of the second click point of this vertex.
     */
    public final double getY2() {
        return y2;
    }

    /** Sets the y coordinate of the third click point of this vertex.
     * @param newy3 The y coordinate of the third click point of this vertex.
     */
    public final void setY3(double newy3) {
        this.y3 = newy3;
    }

    /** Returns the y coordinate of the third click point of this vertex.
     * @return The y coordinate of the third click point of this vertex.
     */
    public final double getY3() {
        return y3;
    }

    /** Sets the points of this vertex.
     * @param sx1 The x coordinate of the first click point of this vertex.
     * @param sy1 The y coordinate of the first click point of this vertex.
     * @param sx2 The x coordinate of the second click point of this vertex.
     * @param sy2 The y coordinate of the second click point of this vertex.
     * @param sx3 The x coordinate of the third click point of this vertex.
     * @param sy3 The y coordinate of the third click point of this vertex.
     */
    public final void setVertexPts(double sx1, double sy1, double sx2,
        double sy2, double sx3, double sy3) {
        this.setX1(sx1);
        this.setY1(sy1);
        this.setX2(sx2);
        this.setY2(sy2);
        this.setX3(sx3);
        this.setY3(sy3);
    }

    /** Returns an exact copy of this JaVertexT1.
     * @return A copy of this JaVertexT1.
     */
    public final JaObject copy() {
        JaVertexT5 temp = new JaVertexT5();

        temp.setVertexPts(getX1(), getY1(), getX2(), getY2(), getX3(), getY3());
        temp.setX(getX());
        temp.setY(getY());
        temp.setColor(getColor());
        temp.setFillColor(getFillColor());
        temp.setStroke(getStroke());
        temp.setSize(getWidth(), getHeight(), getRelw(), getRelh());
        temp.setBoundingBox(getBoundingBox());

        return temp;
    }

    /** Returns true if all serializable variables of this JaObject
     *  and those of the specified one are equal.
     * @param comp A JaObject to compare with.
     * @return True if the objects are equal, false otherwise.
     */
    public final boolean isCopy(JaObject comp) {
        boolean isCopy = false;

        if (comp instanceof JaVertexT5) {
            JaVertexT5 vertex = (JaVertexT5) comp;
            if ((vertex.getX() == getX()) && (vertex.getY() == getY())
                    && (vertex.getX1() == getX1())
                    && (vertex.getY1() == getY1())
                    && (vertex.getX2() == getX2())
                    && (vertex.getY2() == getY2())
                    && (vertex.getX3() == getX3())
                    && (vertex.getY3() == getY3())
                    && vertex.getColor().equals(getColor())
                    && vertex.getFillColor().equals(getFillColor())
                    && (vertex.getStroke() == getStroke())
                    && (vertex.getRelw() == getRelw())
                    && (vertex.getRelh() == getRelh())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** Determines which handle the user has selected to move/resize/edit this
     *  vertex.
     * @param clickX The x coordinate where the mouse click has ocurred.
     * @param clickY The y coordinate where the mouse click has ocurred.
     * @param editmode The current edit mode.
     * @return One of the static variables SELECT_* defined in JaObject
     *  that specifies the handle which the user has clicked.
     */
    public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
        int ax1 = (int) Math.round(getX1());
        int ay1 = (int) Math.round(getY1());
        int ax2 = (int) Math.round(getX2());
        int ay2 = (int) Math.round(getY2());
        int ax3 = (int) Math.round(getX3());
        int ay3 = (int) Math.round(getY3());

        if ((editmode == MOVE) || (editmode == COPY)) {
            if ((clickX >= (ax1 - (LENGTH / 2)))
                    && (clickX <= (ax1 + (LENGTH / 2)))
                    && (clickY >= (ay1 - (LENGTH / 2)))
                    && (clickY <= (ay1 + (LENGTH / 2)))) {
                return SELECT_BODY;
            } else if ((clickX >= (ax2 - (LENGTH / 2)))
                    && (clickX <= (ax2 + (LENGTH / 2)))
                    && (clickY >= (ay2 - (LENGTH / 2)))
                    && (clickY <= (ay2 + (LENGTH / 2)))) {
                return SELECT_BODY;
            } else if ((clickX >= (ax3 - (LENGTH / 2)))
                    && (clickX <= (ax3 + (LENGTH / 2)))
                    && (clickY >= (ay3 - (LENGTH / 2)))
                    && (clickY <= (ay3 + (LENGTH / 2)))) {
                return SELECT_BODY;
            }
        } else if (editmode == RESIZE) {
            if ((clickX >= (ax1 - (LENGTH / 2)))
                    && (clickX <= (ax1 + (LENGTH / 2)))
                    && (clickY >= (ay1 - (LENGTH / 2)))
                    && (clickY <= (ay1 + (LENGTH / 2)))) {
                return SELECT_P1;
            } else if ((clickX >= (ax2 - (LENGTH / 2)))
                    && (clickX <= (ax2 + (LENGTH / 2)))
                    && (clickY >= (ay2 - (LENGTH / 2)))
                    && (clickY <= (ay2 + (LENGTH / 2)))) {
                return SELECT_P2;
            } else if ((clickX >= (ax3 - (LENGTH / 2)))
                    && (clickX <= (ax3 + (LENGTH / 2)))
                    && (clickY >= (ay3 - (LENGTH / 2)))
                    && (clickY <= (ay3 + (LENGTH / 2)))) {
                return SELECT_P3;
            }
        }

        return SELECT_NONE;
    }

    /** Draws the handles of this vertex object.
     * @param g2 The current graphics context.
     */
    public final void drawHandles(VectorGraphics g2) {
        int ax1 = (int) Math.round(getX1());
        int ay1 = (int) Math.round(getY1());
        int ax2 = (int) Math.round(getX2());
        int ay2 = (int) Math.round(getY2());
        int ax3 = (int) Math.round(getX3());
        int ay3 = (int) Math.round(getY3());

        g2.setColor(JaxoColor.RED);
        g2.drawRect(ax1 - (LENGTH / 2), ay1 - (LENGTH / 2), LENGTH, LENGTH);
        g2.drawRect(ax2 - (LENGTH / 2), ay2 - (LENGTH / 2), LENGTH, LENGTH);
        g2.drawRect(ax3 - (LENGTH / 2), ay3 - (LENGTH / 2), LENGTH, LENGTH);

        if (isMarked()) {
            g2.setColor(JaxoColor.GRAYSCALE150);
            g2.fillRect(ax1 - (LENGTH / 2) + 1, ay1 - (LENGTH / 2) + 1,
                LENGTH - 1, LENGTH - 1);
            g2.fillRect(ax2 - (LENGTH / 2) + 1, ay2 - (LENGTH / 2) + 1,
                LENGTH - 1, LENGTH - 1);
            g2.fillRect(ax3 - (LENGTH / 2) + 1, ay3 - (LENGTH / 2) + 1,
                LENGTH - 1, LENGTH - 1);
        }
    }

    /** Draws a visual aid during the dragging of a vertex object,
     *  which is a red cross at the center.
     * @param g2 The graphics context to draw to
     */
    public final void drawVisualAid(VectorGraphics g2) {
        g2.setColor(JaxoColor.RED);
        g2.setStroke(new BasicStroke(1.f));

        float ax1 = (float) Math.round(getX1());
        float ay1 = (float) Math.round(getY1());
        float ax2 = (float) Math.round(getX2());
        float ay2 = (float) Math.round(getY2());
        float ax3 = (float) Math.round(getX3());
        float ay3 = (float) Math.round(getY3());

        GeneralPath gpa = new GeneralPath();
        gpa.moveTo(ax1, ay1);
        gpa.lineTo(ax2, ay2);
        gpa.lineTo(ax3, ay3);
        gpa.closePath();

        g2.draw(gpa);
    }

    /** The method that draws this JaVertexT5.
     * @param g2 The graphics context where the JaVertexT5 has to be drawn.
     * @param drawToScreen A boolean variable that indicates whether
     * the drawing is done on the screen or somewhere else. This is used
     * for exporting/printing, where the object handles should not be painted,
     * even if they are visible on the screen.
     */
    public final void jaxoDraw(VectorGraphics g2, boolean drawToScreen) {
        g2.setColor(getColor());
        g2.setStroke(new BasicStroke(getStroke()));

        GeneralPath gp = getGeneralPath();
        gp.reset();

        int ax1 = (int) Math.round(getX1());
        int ay1 = (int) Math.round(getY1());
        int ax2 = (int) Math.round(getX2());
        int ay2 = (int) Math.round(getY2());
        int ax3 = (int) Math.round(getX3());
        int ay3 = (int) Math.round(getY3());

        gp.moveTo((float) ax1, (float) ay1);
        gp.lineTo((float) ax2, (float) ay2);
        gp.lineTo((float) ax3, (float) ay3);
        gp.closePath();

        g2.setStroke(new BasicStroke(1.f));
        g2.setColor(getFillColor());
        g2.fill(gp);
        g2.draw(gp);

        g2.setColor(getColor());
        g2.setStroke(new BasicStroke(getStroke()));
        g2.draw(gp);

        Rectangle2D bb = gp.getBounds2D();
        double[] bbox =
        {bb.getMinX(), bb.getMinY(), bb.getMaxX(), bb.getMaxY()};
        setBoundingBox(bbox);
    }

    /** Shifts the elements of the vector containing the three corner
     * points of this vertex.
     * @param deltaX The x displacement.
     * @param deltaY The y displacement.
     */
    public final void moveBy(int deltaX, int deltaY) {
        super.moveBy(deltaX, deltaY);

        setVertexPts(getX1() + deltaX, getY1() + deltaY, getX2() + deltaX,
            getY2() + deltaY, getX3() + deltaX, getY3() + deltaY);
    }

    /** The LaTeX command that is necessary to draw the given JaVertexT2
     * using the axodraw.sty package.
     * @param scale A scale factor to translate Java coordinates
     * to LaTeX coordinates.
     * @param canvasDim The current dimension of the canvas.
     * @return The corresponding axodraw LaTeX command.
     */
    public final String latexCommand(float scale, Dimension canvasDim) {
        double ax1 = getX1() / scale;
        double ay1 = (canvasDim.height - getY1()) / scale;
        double ax2 = getX2() / scale;
        double ay2 = (canvasDim.height - getY2()) / scale;
        double ax3 = getX3() / scale;
        double ay3 = (canvasDim.height - getY3()) / scale;

        String command = "";

        if (JaxoColor.getIndex(getFillColor()) < JaxoColor.getIndex(
                    JaxoColor.BLACK)) {
            String tlc = JaxoColor.getColorName(getColor());
            String tfc = JaxoColor.getColorName(getFillColor());

            command =
                "\\CTri" + "(" + D_FORMAT.format(ax1) + ","
                + D_FORMAT.format(ay1) + ")" + "(" + D_FORMAT.format(ax2)
                + "," + D_FORMAT.format(ay2) + ")" + "("
                + D_FORMAT.format(ax3) + "," + D_FORMAT.format(ay3) + ")"
                + "{" + tlc + "}" + "{" + tfc + "}";
        } else {
            String grayScale = JaxoColor.getGreyScale(getFillColor());

            command =
                "\\GTri" + "(" + D_FORMAT.format(ax1) + ","
                + D_FORMAT.format(ay1) + ")" + "(" + D_FORMAT.format(ax2)
                + "," + D_FORMAT.format(ay2) + ")" + "("
                + D_FORMAT.format(ax3) + "," + D_FORMAT.format(ay3) + ")"
                + "{" + grayScale + "}";
        }

        return command;
    }

    /** Rescales this JaVertex by the scale factor scale,
     * keeping the point (orx, ory) fixed.
     * @param orx The x-coordinate of the fixed point
     * @param ory The y-coordinate of the fixed point
     * @param scale The scale parameter
     */
    public final void rescaleObject(int orx, int ory, float scale) {
        setVertexPts((scalePoint(orx, ory, scale, getX1(), getY1())).getX(),
            (scalePoint(orx, ory, scale, getX1(), getY1())).getY(),
            (scalePoint(orx, ory, scale, getX2(), getY2())).getX(),
            (scalePoint(orx, ory, scale, getX2(), getY2())).getY(),
            (scalePoint(orx, ory, scale, getX3(), getY3())).getX(),
            (scalePoint(orx, ory, scale, getX3(), getY3())).getY());
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   private auxiliary methods  //
    //
    /////////////////////////////////////////////////////////////////////////

    /** Brings up the edit panel that allows to change the parameters of
     * this object.
     * @return True if the editing actually changed the object, false if
     * the object has not been changed.
     */
    public final boolean editPanel() {
        JaxoVertexT5OptionsPanel vert5op = new JaxoVertexT5OptionsPanel(this);

        return vert5op.hasChanged();
    }
}

 

Ads help maintain this website.