/*

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


import japlot.jaxodraw.*;

/** A zig-zag line. */
public class JaZigZagLine extends JaLineObject {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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


        setColor(JaxoColor.getColor(JaxoPrefs.getPref(
                    JaxoPrefs.PREF_COLOR)));
        setAmp(JaxoPrefs.getIntPref(JaxoPrefs.PREF_AMPLITUDE));
        setFreq(20.0f);
        setStroke(JaxoPrefs.getFloatPref(
                JaxoPrefs.PREF_LINEWIDTH));
        setSymm(false);
        setDoubleLine(false);
        setDLSeparation(JaxoPrefs.getFloatPref(
                JaxoPrefs.PREF_DLSEP));



    }

    /** Returns an exact copy of this JaZigZagLine.
     * @return A copy of this JaZigZagLine.
     */
    public final JaObject copy() {
        JaZigZagLine temp = new JaZigZagLine();
        temp.setX(getX());
        temp.setY(getY());
        temp.setAmp(getAmp());
        temp.setFreq(getFreq());
        temp.setColor(getColor());
        temp.setStroke(getStroke());
        temp.setSymm(isSymm());
        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 JaZigZagLine) {
            JaZigZagLine line = (JaZigZagLine) comp;
            if ((line.getX() == getX()) && (line.getY() == getY())
                    && (line.getAmp() == getAmp())
                    && (line.getFreq() == getFreq())
                    && line.getColor().equals(getColor())
                    && (line.getStroke() == getStroke())
                    && (line.isSymm() == isSymm())
                    && (line.getDoubleLine() == getDoubleLine())
                    && (line.getDLSeparation() == getDLSeparation())
                    && (line.getRelw() == getRelw())
                    && (line.getRelh() == getRelh())) {
                isCopy = true;
            }
        }

        return isCopy;

    }

    /** The method that draws this JaZigZagLine.
     * @param g2 The graphics context where the JaZigZagLine 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 a = (int) Math.round(getAmp() / 1.5f);
        int n;
        float ts;
        double length =
            Math.sqrt(((double) getWidth() * (double) getWidth())
                + ((double) getHeight() * (double) getHeight()));

        n = 2 * ((int) Math.round((length / getFreq())));

        if (getSymm()) {
            n = n + 1;
        }

        ts = (float) (length / n);

        if (getDoubleLine()) {
            gp.moveTo(0.f, getDLSeparation());
            for (int i = 1; i <= n; i++) {
                gp.lineTo((((2 * i) - 1) * ts) / 2.f,
                    (float) -a + getDLSeparation());
                gp.lineTo(i * ts, getDLSeparation());
                a = -a;
            }

            gp.moveTo(0.f, -getDLSeparation());
            for (int i = 1; i <= n; i++) {
                gp.lineTo((((2 * i) - 1) * ts) / 2.f,
                    (float) -a - getDLSeparation());
                gp.lineTo(i * ts, -getDLSeparation());
                a = -a;
            }
        } else {
            gp.moveTo(0.f, 0.f);

            for (int i = 1; i <= n; i++) {
                gp.lineTo((((2 * i) - 1) * ts) / 2.f, (float) -a);
                gp.lineTo(i * ts, 0.f);
                a = -a;
            }
        }

        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 this JaZigZagLine
     * 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 amplitude = getLaTexAmplitude(2.f * scale);
        float wiggles = getLaTexWiggleNumber(1.0f);

        String command = "";
        String base = "\\ZigZag";

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

            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(amplitude) + "}" + "{"
                + D_FORMAT.format(wiggles) + "}";
            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(amplitude) + "}" + "{"
                + D_FORMAT.format(wiggles) + "}";

            command =
                command1.concat(command2.concat("%%JaxoDrawID:DoubleLine"
                        + "(" + D_FORMAT.format(getDLSeparation()) + ")"));
        } 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(amplitude) + "}" + "{"
                + D_FORMAT.format(wiggles) + "}";
        }

        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 getLaTexAmplitude(float scaleFactor) {
        float a = getAmp() / scaleFactor;

        return a;
    }

    private float getLaTexWiggleNumber(float scaleFactor) {
        double length =
            Math.sqrt(((double) getWidth() * (double) getWidth())
                + ((double) getHeight() * (double) getHeight()));

        float n = Math.round(length / getFreq());

        if (getSymm()) {
            n = n + 0.5f;
        }

        n = n / scaleFactor;

        return n;
    }

    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.ZIGZAG);
        lineop.initParams(this);
        lineop.setPanelIcon("japlot/jaxodraw/icons/zigzag.png");
        lineop.setPanelTitle("Zigzag_line_parameters");
        baseEditPanel(lineop);
        lineop.addFinalAwSPanel(1, 0);
        lineop.addDlPanel(2, 0);
        lineop.addLineSymmPanel(2, 1);

        boolean hasChanged = lineop.showPanel();

        return hasChanged;
    }
}

 

Ads help maintain this website.