Java source code of 'jhplot.jadraw.JaFArc'

/*

 * 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 java.awt.BasicStroke;
import java.awt.Dimension;
import org.freehep.graphics2d.VectorGraphics;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;



/** A fermion arc. */
public class JaFArc extends JaArcObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Constructor: just calls super(). */
    public JaFArc() {
        super();
    }

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

        temp.setArcPts(getX1(), getY1(), getX2(), getY2(), getX(), getY());
        temp.setX(getX());
        temp.setY(getY());
        temp.setColor(getColor());
        temp.setStroke(getStroke());
        temp.setArrow(isArrow());
        temp.setEnd(isEnd());
        temp.setFlip(isFlip());
        temp.setStangle(getStangle());
        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 JaFArc
     *  and those of the specified one are equal.
     * @param comp A JaFArc to compare with.
     * @return True if the arcs are equal, false otherwise.
     */
    public final boolean isCopy(JaObject comp) {
        boolean isCopy = false;

        if (comp instanceof JaFArc) {
            JaFArc arc = (JaFArc) comp;
            if ((arc.getX1() == getX1()) && (arc.getX2() == getX2())
                    && (arc.getY1() == getY1()) && (arc.getY2() == getY2())
                    && (arc.getX() == getX()) && (arc.getY() == getY())
                    && (arc.getColor().equals(getColor()))
                    && (arc.getStroke() == getStroke())
                    && (arc.isArrow() == isArrow())
                    && (arc.isEnd() == isEnd()) && (arc.isFlip() == isFlip())
                    && (arc.getStangle() == getStangle())
                    && (arc.getDoubleLine() == getDoubleLine())
                    && (arc.getDLSeparation() == getDLSeparation())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** The method that draws this JaFArc.
     * @param g2 The graphics context where the JaFArc 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) {
        GeneralPath gp = getGeneralPath();
        if (testArea()) {
            return;
        } else {
            g2.setColor(getColor());
            g2.setStroke(new BasicStroke(getStroke()));
            GeneralPath gptmp = new GeneralPath();
            gp.reset();
            double[] par = getArcParameters();

            if (isArrow() && !isResizing()) {
                if (getDoubleLine()) {
                    gp = dArcArrow(g2, getColor(), par[2],
                            (float) 3 * getStroke(), isFlip(),
                            getDLSeparation());

                    if (!isEnd()) {
                        gp = newtransarc(gp, par[0], par[1],
                                ((Math.PI - par[3]) / 2.d) + par[4], 1.f);
                    } else {
                        if (isFlip()) {
                            gp = newtransarc(gp, par[0], par[1],
                                    ((Math.PI - par[3]) / 2.d) + par[4]
                                    + (par[3] / 2.d), 1.f);
                        } else {
                            gp = newtransarc(gp, par[0], par[1],
                                    (((Math.PI - par[3]) / 2.d) + par[4])
                                    - (par[3] / 2.d), 1.f);
                        }
                    }

                    g2.fill(gp);
                    gptmp.append(gp, false);

                    gp.reset();

                    gp = dArcArrow(g2, getColor(), par[2],
                            (float) 3 * getStroke(), isFlip(),
                            -getDLSeparation());

                    if (!isEnd()) {
                        gp = newtransarc(gp, par[0], par[1],
                                ((Math.PI - par[3]) / 2.d) + par[4], 1.f);
                    } else {
                        if (isFlip()) {
                            gp = newtransarc(gp, par[0], par[1],
                                    ((Math.PI - par[3]) / 2.d) + par[4]
                                    + (par[3] / 2.d), 1.f);
                        } else {
                            gp = newtransarc(gp, par[0], par[1],
                                    (((Math.PI - par[3]) / 2.d) + par[4])
                                    - (par[3] / 2.d), 1.f);
                        }
                    }

                    g2.fill(gp);
                    gptmp.append(gp, false);
                } else {
                    gp = arcArrow(g2, getColor(), par[2],
                            (float) 4 * getStroke(), isFlip());

                    if (!isEnd()) {
                        gp = newtransarc(gp, par[0], par[1],
                                ((Math.PI - par[3]) / 2.d) + par[4], 1.f);
                    } else {
                        if (isFlip()) {
                            gp = newtransarc(gp, par[0], par[1],
                                    ((Math.PI - par[3]) / 2.d) + par[4]
                                    + (par[3] / 2.d), 1.f);
                        } else {
                            gp = newtransarc(gp, par[0], par[1],
                                    (((Math.PI - par[3]) / 2.d) + par[4])
                                    - (par[3] / 2.d), 1.f);
                        }
                    }

                    g2.fill(gp);
                    gptmp.append(gp, false);
                }
            }

            if (isResizing()) {
                g2.setColor(JaxoColor.RED);
                g2.setStroke(new BasicStroke(1.f));
            } else {
                g2.setStroke(new BasicStroke(getStroke()));
            }

            gp.reset();
            gp.moveTo(0.f, 0.f);

            Arc2D arc = new Arc2D.Double();

            if (getDoubleLine()) {
                arc.setArc(-par[2] - getDLSeparation(),
                    -par[2] - getDLSeparation(),
                    2 * (par[2] + getDLSeparation()),
                    2 * (par[2] + getDLSeparation()), 0,
                    Math.toDegrees(par[3]), Arc2D.OPEN);
                gp.append(arc, false);
                arc.setArc(-par[2] + getDLSeparation(),
                    -par[2] + getDLSeparation(),
                    2 * (par[2] - getDLSeparation()),
                    2 * (par[2] - getDLSeparation()), 0,
                    Math.toDegrees(par[3]), Arc2D.OPEN);
                gp.append(arc, false);
            } else {
                arc.setArc(-par[2], -par[2], 2 * par[2], 2 * par[2], 0,
                    Math.toDegrees(par[3]), Arc2D.OPEN);
                gp.append(arc, false);
            }

            gp = newtransarc(gp, par[0], par[1], par[4], 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 JaFArc
     * 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[] par = getArcParameters();
        double cx = par[0] / scale;
        double cy = (canvasDim.height - par[1]) / scale;
        double r = par[2] / scale;
        double sa = -Math.toDegrees(par[4]);
        double ea = Math.toDegrees(par[3] - par[4]);

        String command = "";
        String base = "\\CArc";

        if (isArrow()) {
            if (isEnd()) {
                if (isFlip()) {
                    base = "\\LongArrowArcn";

                    double swap;
                    swap = ea;
                    ea = sa;
                    sa = swap;
                } else {
                    base = "\\LongArrowArc";
                }
            } else {
                if (isFlip()) {
                    base = "\\ArrowArcn";

                    double swap;
                    swap = ea;
                    ea = sa;
                    sa = swap;
                } else {
                    base = "\\ArrowArc";
                }
            }
        }

        if (getDoubleLine()) {
            float offSet = getDLSeparation() / scale;

            String command1 =
                base + "(" + D_FORMAT.format(cx) + "," + D_FORMAT.format(cy)
                + ")" + "(" + D_FORMAT.format(r + offSet) + ","
                + D_FORMAT.format(sa) + "," + D_FORMAT.format(ea) + ")";

            String command2 =
                base + "(" + D_FORMAT.format(cx) + "," + D_FORMAT.format(cy)
                + ")" + "(" + D_FORMAT.format(r - offSet) + ","
                + D_FORMAT.format(sa) + "," + D_FORMAT.format(ea) + ")";

            command =
                command1.concat(command2.concat("%%JaxoDrawID:DoubleLine"
                        + "(" + D_FORMAT.format(getDLSeparation()) + ")"));
        } else {
            command =
                base + "(" + D_FORMAT.format(cx) + "," + D_FORMAT.format(cy)
                + ")" + "(" + D_FORMAT.format(r) + "," + D_FORMAT.format(sa)
                + "," + D_FORMAT.format(ea) + ")";
        }

        return command;
    }
}