/*
* 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.Point2D;
import japlot.jaxodraw.JaxoPrefs;
/** A vertex object. */
public abstract class JaVertex extends JaObject {
/** The stroke (line width) of this object. */
private float stroke;
/** Constructor: sets the width, height, relative width and height,
* and stroke to a default value of 2.
*/
public JaVertex() {
setRelw(2);
setRelh(2);
setStroke(1.f);
setColor(JaxoColor.getColor(JaxoPrefs.getPref(
JaxoPrefs.PREF_COLOR)));
}
/** Returns the stroke property of this line object.
* @return The stroke property of this line object.
*/
public final float getStroke() {
return stroke;
}
/** Sets the stroke property of this line object.
* @param newStroke The stroke property of this line object.
*/
public final void setStroke(float newStroke) {
this.stroke = newStroke;
}
/** Returns an exact copy of this JaVertex.
* @param temp An instance of the vertex to copy to.
* @return A copy of this JaVertex.
*/
public final JaObject copy(JaVertex temp) {
temp.setX(getX());
temp.setY(getY());
temp.setColor(getColor());
temp.setStroke(getStroke());
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 boolean isCopy(JaObject comp) {
boolean isCopy = false;
if (comp instanceof JaVertex) {
if ((((JaVertex) comp).getX() == getX())
&& (((JaVertex) comp).getY() == getY())
&& ((JaVertex) comp).getColor().equals(getColor())
&& (((JaVertex) comp).getStroke() == getStroke())
&& (((JaVertex) comp).getRelw() == getRelw())
&& (((JaVertex) comp).getRelh() == getRelh())) {
isCopy = true;
}
}
return isCopy;
}
/** Determines which handle the user has selected to move/resize/edit this
* vertex.
* @param clickX The x coordinate where the mouse click has ocurred.
* @param clickY The y coordinate where the mouse click has ocurred.
* @param editmode The current edit mode.
* @return One of the static variables SELECT_* defined in JaObject
* that specifies the handle which the user has clicked.
*/
public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
int x = getX();
int y = getY();
int w = getWidth();
int h = getHeight();
int l2 = LENGTH / 2;
if ((editmode == MOVE) || (editmode == COPY)) {
if ((getRelw() > 0) && (getRelh() >= 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_BODY;
}
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_BODY;
}
}
if ((getRelw() < 0) && (getRelh() <= 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_BODY;
}
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_BODY;
}
}
if ((getRelw() >= 0) && (getRelh() < 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_BODY;
}
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_BODY;
}
}
if ((getRelw() <= 0) && (getRelh() > 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_BODY;
}
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_BODY;
}
}
return SELECT_NONE;
} else if (editmode == RESIZE) {
if ((getRelw() > 0) && (getRelh() >= 0)) {
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_LR;
}
} else if ((getRelw() < 0) && (getRelh() <= 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_UL;
}
} else if ((getRelw() >= 0) && (getRelh() < 0)) {
if ((clickX >= ((x + w) - l2)) && (clickX <= (x + w + l2))
&& (clickY >= (y - l2)) && (clickY <= (y + l2))) {
return SELECT_UR;
}
} else if ((getRelw() <= 0) && (getRelh() > 0)) {
if ((clickX >= (x - l2)) && (clickX <= (x + l2))
&& (clickY >= ((y + h) - l2))
&& (clickY <= (y + h + l2))) {
return SELECT_LL;
}
}
return SELECT_NONE;
}
return SELECT_NONE;
}
/** Draws the handles of this vertex object.
* @param g2 The current graphics context.
*/
public final void drawHandles(VectorGraphics g2) {
g2.setColor(JaxoColor.RED);
g2.setStroke(new BasicStroke(1.3f));
int x = getX();
int y = getY();
int w = getWidth();
int h = getHeight();
int l2 = LENGTH / 2;
int lm1 = LENGTH - 1;
if (((getRelw() > 0) && (getRelh() >= 0))
|| ((getRelw() < 0) && (getRelh() <= 0))) {
g2.drawRect(x - l2, y - l2, LENGTH, LENGTH);
g2.drawRect((x + w) - l2, (y + h) - l2, LENGTH, LENGTH);
if (isMarked()) {
g2.setColor(JaxoColor.GRAYSCALE150);
g2.fillRect(x - l2 + 1, y - l2 + 1, lm1, lm1);
g2.fillRect((x + w) - l2 + 1, (y + h) - l2 + 1, lm1, lm1);
}
}
if (((getRelw() <= 0) && (getRelh() > 0))
|| ((getRelw() >= 0) && (getRelh() < 0))) {
g2.drawRect(x - l2, (y + h) - l2, LENGTH, LENGTH);
g2.drawRect((x + w) - l2, y - l2, LENGTH, LENGTH);
if (isMarked()) {
g2.setColor(JaxoColor.GRAYSCALE150);
g2.fillRect(x - l2 + 1, (y + h) - l2 + 1, lm1, lm1);
g2.fillRect((x + w) - l2 + 1, y - l2 + 1, lm1, lm1);
}
}
}
/** Draws a visual aid during the dragging of a vertex object,
* which is a red cross at the center.
* @param g2 The graphics context to draw to
*/
public final void drawVisualAid(VectorGraphics g2) {
g2.setColor(JaxoColor.RED);
g2.setStroke(new BasicStroke(1.3f));
int x = getX();
int y = getY();
int w = getWidth();
int h = getHeight();
int lp4 = LENGTH + 4;
if ((getRelw() > 0) && (getRelh() >= 0)) {
g2.drawLine(x - lp4, y, x + lp4, y);
g2.drawLine(x, y - lp4, x, y + lp4);
} else if ((getRelw() <= 0) && (getRelh() > 0)) {
g2.drawLine((x + w) - lp4, y, x + w + lp4, y);
g2.drawLine(x + w, y - lp4, x + w, y + lp4);
} else if ((getRelw() < 0) && (getRelh() <= 0)) {
g2.drawLine(x + w, (y + h) - lp4, x + w, y + h + lp4);
g2.drawLine((x + w) - lp4, y + h, x + w + lp4, y + h);
} else if ((getRelw() >= 0) && (getRelh() < 0)) {
g2.drawLine(x - lp4, y + h, x + lp4, y + h);
g2.drawLine(x, (y + h) - lp4, x, y + h + lp4);
}
}
/** Returns the line width of this JaVertex.
* @return The line width of this JaVertex.
*/
public final String latexWidth() {
// a default stroke value of 1.0 corresponds to a
// default width in axodraw of 0.5;
String thisValue = Float.toString(getStroke() / 2.f);
String base = "\\SetWidth{";
String closing = "}";
String lineWidth = base.concat(thisValue.concat(closing));
return lineWidth;
}
/** Rescales this JaVertex by the scale factor scale,
* keeping the point (orx, ory) fixed.
* @param orx The x-coordinate of the fixed point
* @param ory The y-coordinate of the fixed point
* @param scale The scale parameter
*/
public final void rescaleObject(int orx, int ory, float scale) {
//int oldWidth = this.getSize().width;
//int oldHeight = this.getSize().height;
//int newWidth = (int) Math.round(oldWidth * scale);
//int newHeight = (int) Math.round(oldHeight * scale);
int newRelWidth = (int) Math.round(this.getRelSize().width * scale);
int newRelHeight = (int) Math.round(this.getRelSize().height * scale);
Point2D newP =
this.scalePoint(orx, ory, scale, this.getX(), this.getY());
this.setX((int) Math.round(newP.getX()));
this.setY((int) Math.round(newP.getY()));
//this.setWAndH(newWidth, newHeight);
this.setRelWAndH(newRelWidth, newRelHeight);
}
/** Returns a center point which is used by the latexCommand method.
* @param scaleFactor A scale factor.
* @param canvasHeight The height of the current canvas.
* @return A Point2D object holding the center point.
*/
protected Point2D getLaTexCenter(float scaleFactor, int canvasHeight) {
Point2D centerPoint = new Point2D.Float();
float cx;
float cy;
if ((getRelw() > 0) && (getRelh() >= 0)) {
cx = getX() / scaleFactor;
cy = (canvasHeight - getY()) / scaleFactor;
centerPoint.setLocation(cx, cy);
} else if ((getRelw() >= 0) && (getRelh() < 0)) {
cx = getX() / scaleFactor;
cy = (canvasHeight - (getY() + getHeight())) / scaleFactor;
centerPoint.setLocation(cx, cy);
} else if ((getRelw() < 0) && (getRelh() <= 0)) {
cx = (getX() + getWidth()) / scaleFactor;
cy = (canvasHeight - (getY() + getHeight())) / scaleFactor;
centerPoint.setLocation(cx, cy);
} else if ((getRelw() <= 0) && (getRelh() > 0)) {
cx = (getX() + getWidth()) / scaleFactor;
cy = (canvasHeight - getY()) / scaleFactor;
centerPoint.setLocation(cx, cy);
}
return centerPoint;
}
/** Returns a radius which is used by the latexCommand method.
* @param scaleFactor A scale factor.
* @return The latex radius.
*/
protected float getLaTexRadius(float scaleFactor) {
float length =
(float) Math.sqrt(((double) getWidth() * (double) getWidth())
+ ((double) getHeight() * (double) getHeight()));
length = length / scaleFactor;
return length;
}
// These methods are abstract and all subclasses must implement
// them to be instantiated.
/** The method that draws the JaObject.
* @param g2 The graphics context where the object 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 abstract void jaxoDraw(VectorGraphics g2, boolean drawToScreen);
/** The LaTeX command that is necessary to draw the given JaObject
* 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 abstract String latexCommand(float scale, Dimension canvasDim);
/** 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 abstract boolean editPanel();
}
Ads help maintain this website.