Java source code of 'jhplot.jadraw.JaSLoop'

/*

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



/** A scalar loop. */
public class JaSLoop extends JaLoopObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

    /** Returns an exact copy of this JaSLoop.
     * @return A copy of this JaSLoop.
     */
    public final JaObject copy() {
        JaSLoop temp = (JaSLoop) copy(new JaSLoop());
        temp.setDash(getDash());
        temp.setArrow(isArrow());
        temp.setFlip(isFlip());

        return temp;
    }

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

        if (comp instanceof JaSLoop) {
            comp = (JaSLoop) comp;
            if (super.isCopy(comp)
                    && (((JaSLoop) comp).getDash() == getDash())
                    && (((JaSLoop) comp).isArrow() == isArrow())
                    && (((JaSLoop) comp).isFlip() == isFlip())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** The method that draws this JaSLoop.
     * @param g2 The graphics context where the JaSLoop 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 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 = dLoopArrow(g2, getColor(), length,
                        (float) 3 * getStroke(), getFlip(), getDLSeparation());
                gptmp.append(gp, false);
                gp.reset();
                gp = dLoopArrow(g2, getColor(), length,
                        (float) 3 * getStroke(), getFlip(), -getDLSeparation());
                gptmp.append(gp, false);
            } else {
                gp = loopArrow(g2, getColor(), length,
                        (float) 4 * getStroke(), getFlip());
                gptmp.append(gp, false);
            }
        }

        gp.reset();

        Ellipse2D loop = new Ellipse2D.Double();
        if (getDoubleLine()) {
            float cf1 = (float) ((length + getDLSeparation()) / length);
            float cf2 = (float) ((length - getDLSeparation()) / length);
            float[] dashes1 = {cf1 * getDash()};
            float[] dashes2 = {cf2 * getDash()};

            loop.setFrame(-length + getDLSeparation(),
                -length + getDLSeparation(), 2 * (length - getDLSeparation()),
                2 * (length - getDLSeparation()));
            gp.append(loop, false);
            gp = transv(gp, 1.f);
            g2.setStroke(new BasicStroke(getStroke(), BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_MITER, 10.0f, dashes2, 0.0f));
            gptmp.append(gp, false);
            gp.reset();
            loop.setFrame(-length - getDLSeparation(),
                -length - getDLSeparation(), 2 * (length + getDLSeparation()),
                2 * (length + getDLSeparation()));
            gp.append(loop, false);
            gp = transv(gp, 1.f);
            g2.setStroke(new BasicStroke(getStroke(), BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_MITER, 10.0f, dashes1, 0.0f));
            gptmp.append(gp, false);
        } else {
            float[] dashes = {getDash()};
            loop.setFrame(-length, -length, 2 * length, 2 * length);
            gp.append(loop, false);
            gp = transv(gp, 1.f);
            g2.setStroke(new BasicStroke(getStroke(), BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_MITER, 10.0f, dashes, 0.0f));
            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 JaSLoop
     * 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 center = getLaTexCenter(scale, canvasHeight);
        float radius = getLaTexRadius(scale);
        Point2D angles = getLaTexAngles();
        float dashSize = getLaTexDashSize(scale);
        String command = "";
        String base = "\\DashCArc";

        if (isArrow()) {
            if (isFlip()) {
                base = "\\DashArrowArcn";
            } else {
                base = "\\DashArrowArc";
            }
        }

        if ((int) radius == 0) {
            command = "%";
        } else if (getDoubleLine()) {
            float offSet = getDLSeparation() / scale;
            float dashSize1 = ((radius + offSet) / radius) * dashSize;
            float dashSize2 = ((radius - offSet) / radius) * dashSize;

            String command1 =
                base + "(" + D_FORMAT.format(center.getX()) + ","
                + D_FORMAT.format(center.getY()) + ")" + "("
                + D_FORMAT.format(radius + offSet) + ","
                + D_FORMAT.format(angles.getX()) + ","
                + D_FORMAT.format(angles.getY()) + ")" + "{"
                + D_FORMAT.format(dashSize1) + "}";

            String command2 =
                base + "(" + D_FORMAT.format(center.getX()) + ","
                + D_FORMAT.format(center.getY()) + ")" + "("
                + D_FORMAT.format(radius - offSet) + ","
                + D_FORMAT.format(angles.getX()) + ","
                + D_FORMAT.format(angles.getY()) + ")" + "{"
                + D_FORMAT.format(dashSize2) + "}";

            command =
                command1.concat(command2.concat("%%JaxoDrawID:DoubleLine"
                        + "(" + D_FORMAT.format(getDLSeparation()) + ")"));
        } else {
            command =
                base + "(" + D_FORMAT.format(center.getX()) + ","
                + D_FORMAT.format(center.getY()) + ")" + "("
                + D_FORMAT.format(radius) + ","
                + D_FORMAT.format(angles.getX()) + ","
                + D_FORMAT.format(angles.getY()) + ")" + "{"
                + D_FORMAT.format(dashSize) + "}";
        }

        return command;
    }

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

        return dshSize;
    }
}