Java source code of 'jhplot.jadraw.JaSLine'

/*

 * 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.JaxoLineOptionsPanel;
import japlot.jaxodraw.JaxoMainPanel;

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



/** A scalar line. */
public class JaSLine extends JaLineObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Constructor: calls super() and initializes inflip to false.*/
    public JaSLine() {
        super();
        setInflip(false);
    }

    /** Returns an exact copy of this JaSLine.
     * @return A copy of this JaSLine.
     */
    public final JaObject copy() {
        JaSLine temp = new JaSLine();
        temp.setX(getX());
        temp.setY(getY());
        temp.setColor(getColor());
        temp.setStroke(getStroke());
        temp.setDash(getDash());
        temp.setArrow(isArrow());
        temp.setFlip(isFlip());
        temp.setInflip(getInflip());
        temp.setDoubleLine(getDoubleLine());
        temp.setDLSeparation(getDLSeparation());
        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 JaSLine) {
            JaSLine line = (JaSLine) comp;
            if ((line.getX() == getX()) && (line.getY() == getY())
                    && line.getColor().equals(getColor())
                    && (line.getStroke() == getStroke())
                    && (line.getDash() == getDash())
                    && (line.isArrow() == isArrow())
                    && (line.isFlip() == isFlip())
                    && (line.getInflip() == getInflip())
                    && (line.getDoubleLine() == getDoubleLine())
                    && (line.getDLSeparation() == getDLSeparation())
                    && (line.getRelw() == getRelw())
                    && (line.getRelh() == getRelh())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** The method that draws this JaSLine.
     * @param g2 The graphics context where the JaSLine 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());

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

        double length =
            Math.sqrt(((double) getWidth() * (double) getWidth())
                + ((double) getHeight() * (double) getHeight()));

        if (isArrow()) {
            if (getDoubleLine()) {
                gp = dLineArrow(g2, getColor(), length,
                        (float) 3 * getStroke(), getInflip(), getDLSeparation());
                gptmp.append(gp, false);
                gp.reset();
                gp = dLineArrow(g2, getColor(), length,
                        (float) 3 * getStroke(), getInflip(),
                        -getDLSeparation());
                gptmp.append(gp, false);
            } else {
                gp = lineArrow(g2, getColor(), length,
                        (float) 4 * getStroke(), getInflip());
                gptmp.append(gp, false);
            }
        }

        float[] dashes = {getDash()};

        g2.setStroke(new BasicStroke(getStroke(), BasicStroke.CAP_SQUARE,
                BasicStroke.JOIN_MITER, 10.0f, dashes, 0.0f));
        gp.reset();

        if (getDoubleLine()) {
            gp.moveTo(0.f, getDLSeparation());
            gp.lineTo((float) length, getDLSeparation());
            gp = trans(gp, 1.f);
            gptmp.append(gp, false);
            gp.reset();
            gp.moveTo(0.f, -getDLSeparation());
            gp.lineTo((float) length, -getDLSeparation());
            gp = trans(gp, 1.f);
            gptmp.append(gp, false);
        } else {
            gp.moveTo(0.f, 0.f);
            gp.lineTo((float) length, 0.f);
            gp = trans(gp, 1.f);
            gptmp.append(gp, false);
        }

        g2.draw(gptmp);
        Rectangle2D bb = gptmp.getBounds2D();

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

    /** The LaTeX command that is necessary to draw this JaSLine
     * 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) {
        int canvasHeight = canvasDim.height;

        Point2D startPoint = getLaTexStartPoint(scale, canvasHeight);
        Point2D endPoint = getLaTexEndPoint(scale, canvasHeight);
        float dashSize = getLaTexDashSize(scale);
        String command = "";
        String base = "\\DashLine";

        if (isArrow()) {
            base = "\\DashArrowLine";
        }

        if (((int) endPoint.getX() == (int) startPoint.getX())
                && ((int) endPoint.getY() == (int) startPoint.getY())) {
            command = "%";
        } else if (getDoubleLine()) {
            Point2D offSet = getOffSet(scale);

            if (isFlip()) {
                String command1 =
                    base + "("
                    + D_FORMAT.format(endPoint.getX() + offSet.getX()) + ","
                    + D_FORMAT.format(endPoint.getY() + offSet.getY()) + ")"
                    + "(" + D_FORMAT.format(startPoint.getX() + offSet.getX())
                    + "," + D_FORMAT.format(startPoint.getY() + offSet.getY())
                    + ")" + "{" + D_FORMAT.format(dashSize) + "}";

                String command2 =
                    base + "("
                    + D_FORMAT.format(endPoint.getX() - offSet.getX()) + ","
                    + D_FORMAT.format(endPoint.getY() - offSet.getY()) + ")"
                    + "(" + D_FORMAT.format(startPoint.getX() - offSet.getX())
                    + "," + D_FORMAT.format(startPoint.getY() - offSet.getY())
                    + ")" + "{" + D_FORMAT.format(dashSize) + "}";

                command =
                    command1.concat(command2.concat("%%JaxoDrawID:DoubleLine"
                            + "(" + D_FORMAT.format(-getDLSeparation()) + ")"));
            } else {
                String command1 =
                    base + "("
                    + D_FORMAT.format(startPoint.getX() + offSet.getX()) + ","
                    + D_FORMAT.format(startPoint.getY() + offSet.getY()) + ")"
                    + "(" + D_FORMAT.format(endPoint.getX() + offSet.getX())
                    + "," + D_FORMAT.format(endPoint.getY() + offSet.getY())
                    + ")" + "{" + D_FORMAT.format(dashSize) + "}";

                String command2 =
                    base + "("
                    + D_FORMAT.format(startPoint.getX() - offSet.getX()) + ","
                    + D_FORMAT.format(startPoint.getY() - offSet.getY()) + ")"
                    + "(" + D_FORMAT.format(endPoint.getX() - offSet.getX())
                    + "," + D_FORMAT.format(endPoint.getY() - offSet.getY())
                    + ")" + "{" + D_FORMAT.format(dashSize) + "}";

                command =
                    command1.concat(command2.concat("%%JaxoDrawID:DoubleLine"
                            + "(" + D_FORMAT.format(getDLSeparation()) + ")"));
            }
        } else {
            if (isFlip()) {
                command =
                    base + "(" + D_FORMAT.format(endPoint.getX()) + ","
                    + D_FORMAT.format(endPoint.getY()) + ")" + "("
                    + D_FORMAT.format(startPoint.getX()) + ","
                    + D_FORMAT.format(startPoint.getY()) + ")" + "{"
                    + D_FORMAT.format(dashSize) + "}";
            } else {
                command =
                    base + "(" + D_FORMAT.format(startPoint.getX()) + ","
                    + D_FORMAT.format(startPoint.getY()) + ")" + "("
                    + D_FORMAT.format(endPoint.getX()) + ","
                    + D_FORMAT.format(endPoint.getY()) + ")" + "{"
                    + D_FORMAT.format(dashSize) + "}";
            }
        }

        return command;
    }

    /////////////////////////////////////////////////////////////////////////
    //
    //   private auxiliary methods  //
    //
    /////////////////////////////////////////////////////////////////////////
    private Point2D getLaTexStartPoint(float scaleFactor, int canvasHeight) {
        Point2D startVec = new Point2D.Float();
        float x1;
        float y1;

        if (getRelw() <= 0) {
            x1 = getX() - getRelw();
        } else {
            x1 = getX();
        }

        if (getRelh() >= 0) {
            y1 = getY();
        } else {
            y1 = getY() - getRelh();
        }

        startVec.setLocation(x1 / scaleFactor,
            (canvasHeight - y1) / scaleFactor);

        return startVec;
    }

    private Point2D getLaTexEndPoint(float scaleFactor, int canvasHeight) {
        Point2D endVec = new Point2D.Float();
        float x2;
        float y2;

        if (getRelw() >= 0) {
            x2 = getX() + getRelw();
        } else {
            x2 = getX();
        }

        if (getRelh() <= 0) {
            y2 = getY();
        } else {
            y2 = getY() + getRelh();
        }

        endVec.setLocation(x2 / scaleFactor, (canvasHeight - y2) / scaleFactor);

        return endVec;
    }

    private float getLaTexDashSize(float scaleFactor) {
        float dshSize = getDash() / scaleFactor;

        return dshSize;
    }

    private Point2D getOffSet(float scaleFactor) {
        Point2D offSet = new Point2D.Float();
        float lSep = getDLSeparation() / scaleFactor;
        double theta =
            (double) Math.atan2((float) getRelh(), (float) getRelw());

        offSet.setLocation((float) lSep * Math.sin(theta),
            (float) lSep * Math.cos(theta));

        return offSet;
    }

    /** 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() {
        JaxoLineOptionsPanel lineop =
            new JaxoLineOptionsPanel(JaxoMainPanel.SLINE);
        lineop.initParams(this);
        lineop.setPanelIcon("japlot/jaxodraw/icons/sline.gif");
        lineop.setPanelTitle("Scalar_line_parameters");
        baseEditPanel(lineop);
        lineop.addLineArrowPanel(1, 0);
        lineop.addLineMiddlePanel(2, 0);
        lineop.addDlPanel(2, 1);

        boolean hasChanged = lineop.showPanel();

        return hasChanged;
    }
}