Java source code of 'jhplot.jadraw.JaVertexT4'

/*

 * 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.JaxoVertexT4OptionsPanel;

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



/** A vertex (a cross). */
public class JaVertexT4 extends JaVertex {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Constructor: sets the width, height, relative width and height to a
     *  default value of 3 and initializes the firsttime variable to true.
     */
    public JaVertexT4() {
        super();
    }

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

        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 JaVertexT4) {
            isCopy = super.isCopy(comp);
        }

        return isCopy;

    }

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

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

        g2.setColor(getColor());
        g2.setStroke(new BasicStroke(getStroke()));
        gp.moveTo(0.f, -((float) length));
        gp.lineTo(0.f, ((float) length));
        gp.moveTo(-((float) length), 0.f);
        gp.lineTo(((float) length), 0.f);

        gp = trans(gp, 1.f);
        g2.draw(gp);

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

    /** The LaTeX command that is necessary to draw the given JaVertexT4
     * 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;

        String command = "";
        Point2D center = getLaTexCenter(scale, canvasHeight);
        float radius = getLaTexRadius(scale);

        if ((int) radius == 0) {
            command = "%";
        } else {
            Point2D invec1 =
                new Point2D.Float((float) (center.getX() - (radius * 0.707f)),
                    (float) (center.getY() - (radius * 0.707f)));
            Point2D finvec1 =
                new Point2D.Float((float) (center.getX() + (radius * 0.707f)),
                    (float) (center.getY() + (radius * 0.707f)));
            Point2D invec2 =
                new Point2D.Float((float) (center.getX() - (radius * 0.707f)),
                    (float) (center.getY() + (radius * 0.707f)));
            Point2D finvec2 =
                new Point2D.Float((float) (center.getX() + (radius * 0.707f)),
                    (float) (center.getY() - (radius * 0.707f)));

            //             float length =
            //                 (float) Math.sqrt(
            //                     ((double) getWidth() * (double) getWidth())
            //                     + ((double) getHeight() * (double) getHeight()));
            double theta =
                ((double) Math.PI / 4)
                - Math.atan2((float) getRelh(), (float) getRelw());

            AffineTransform at = new AffineTransform();
            at.rotate(theta, center.getX(), center.getY());

            Point2D invect1 = new Point2D.Float();
            Point2D finvect1 = new Point2D.Float();
            Point2D invect2 = new Point2D.Float();
            Point2D finvect2 = new Point2D.Float();

            invect1 = at.transform(invec1, invect1);
            finvect1 = at.transform(finvec1, finvect1);
            invect2 = at.transform(invec2, invect2);
            finvect2 = at.transform(finvec2, finvect2);

            String command1 =
                "\\Line" + "(" + D_FORMAT.format(invect1.getX()) + ","
                + D_FORMAT.format(invect1.getY()) + ")" + "("
                + D_FORMAT.format(finvect1.getX()) + ","
                + D_FORMAT.format(finvect1.getY()) + ")";

            String command2 =
                "\\Line" + "(" + D_FORMAT.format(invect2.getX()) + ","
                + D_FORMAT.format(invect2.getY()) + ")" + "("
                + D_FORMAT.format(finvect2.getX()) + ","
                + D_FORMAT.format(finvect2.getY()) + ")";

            command = command1 + command2;
        }

        return command;
    }

    /** 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() {
        JaxoVertexT4OptionsPanel vert4op = new JaxoVertexT4OptionsPanel(this);

        return vert4op.hasChanged();
    }
}