/*
* 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 graph.RTextLine;
import japlot.jaxodraw.JaxoColor;
import japlot.jaxodraw.JaxoPSTextOptionsPanel;
import japlot.jaxodraw.JaxoStaticCanvasMethods;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import org.freehep.graphics2d.VectorGraphics;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import jplot.Translate;
/**
* Defines a JaObject which is a postscript text. Also handles sub- and
* superscripts, as well as a set of greek characters via a syntax that is
* derived from LaTeX.
*/
public class JaText extends JaObject {
/**
*
*/
private static final long serialVersionUID = 1L;
/** The input text string representing this label object. */
private String textString;
private RTextLine rtext;
private String stext; // short version
private int type;
private int padX;
private int padY;
/** The font of this label object. */
private Font textFont;
private int rotAngle;
private String[] availableFonts;
/**
* Constructor: sets the width, height, relative width and height to a
* default value of 2.
*/
public JaText() {
setRelw(2);
setRelh(2);
setColor(Color.black);
type = 0;
padX = 0;
padY = 0;
this.textFont = new Font("Dialog", Font.BOLD, 16);
rtext = new RTextLine();
}
/**
* Build a text label
*
* @param text
* text
* @param x1
* starting x
* @param y1
* starting y
* @param what
* if "NDC", coordinate in NDC (from 0 to 1), if "USER"
* coordinates are in the user system given by the axes.
*/
public JaText(String text, double x1, double y1, String what) {
setColor(Color.black);
setRelw(2);
setRelh(2);
type = 0;
padX = 0;
padY = 0;
textFont = new Font("Dialog", Font.BOLD, 16);
rtext = new RTextLine();
setLocation(x1, y1, what);
this.textString = text;
parseText();
}
/**
* Set identification type for this text label
*
* @param type
* identification type
*/
public void setType(int type) {
this.type = type;
}
/**
* Get identification type for this label
*
* @return identification type
*/
public int getType() {
return type;
}
/**
* Set pad ID in X
*
* @param pad
* pad id
*/
public void setPadX(int pad) {
this.padX = pad;
}
/**
* Get pad ID in X
*
* @return pad ID
*/
public int getPadX() {
return padX;
}
/**
* Set pad ID in Y
*
* @param pad
* pad id
*/
public void setPadY(int pad) {
this.padY = pad;
}
/**
* Get pad ID in Y
*
* @return pad ID
*/
public int getPadY() {
return padY;
}
/**
* Returns the textFont property of this text object.
*
* @return The textFont property of this text object.
*/
public final Font getFont() {
return textFont;
}
/**
* Sets the textFont property of this text object.
*
* @param font
* The textFont property of this text object.
*/
public final void setFont(Font font) {
this.textFont = font;
rtext.setFont(this.textFont);
}
/**
* Returns the rotAngle property of this text object.
*
* @return The rotAngle property of this text object.
*/
public final int getRotAngle() {
return rotAngle;
}
/**
* Sets the rotAngle property of this text object.
*
* @param angle
* The rotAngle property of this text object.
*/
public final void setRotAngle(int angle) {
this.rotAngle = angle;
}
/**
* Returns the textString property of this text object.
*
* @return The textString property of this text object.
*/
public final String getText() {
return textString;
}
/**
* Parse the text
*/
private final void parseText() {
rtext.setFont(getFont());
rtext.setColor(getColor());
rtext.setText(Translate.decode(textString));
stext = Translate.shrink(textString);
}
/**
* Sets the textString property of this text object.
*
* @param string
* The textString property of this text object.
*/
public final void setText(String string) {
this.textString = string;
parseText();
}
/**
* Returns an exact copy of this label.
*
* @return A copy of this label.
*/
public final JaObject copy() {
JaText temp = new JaText();
temp.setX(getX());
temp.setY(getY());
temp.setPadX(padX);
temp.setPadY(padY);
temp.setRotAngle(getRotAngle());
temp.setColor(getColor());
temp.setText(textString);
temp.setFont(getFont());
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 JaText) {
JaText text = (JaText) comp;
if ((text.getX() == getX()) && (text.getY() == getY())
&& (text.getRotAngle() == getRotAngle())
&& text.getColor().equals(getColor())
&& text.getType() == getType()
&& text.getText().equals(getText())
&& text.getFont().equals(getFont())) {
isCopy = true;
}
}
return isCopy;
}
/**
* Determines where on this JaObject a mouse click has ocurred.
*
* @param clickX
* The x position of the point where the mouse click ocurred.
* @param clickY
* The y position of the point where the mouse click ocurred.
* @param editmode
* The current edit mode as defined in JaxoMainPanel.
* @return An integer specifying whether the click ocurred on one of the
* handles and if yes, on which.
*/
public final int getGrabbedHandle(int clickX, int clickY, int editmode) {
int length = JaObject.LENGTH;
if ((editmode == MOVE) || (editmode == COPY)) {
if ((clickX >= (getX() - (length / 2)))
&& (clickX <= (getX() + (length / 2)))
&& (clickY >= (getY() - (length / 2)))
&& (clickY <= (getY() + (length / 2)))) {
return SELECT_BODY;
} else {
return SELECT_NONE;
}
} else if (editmode == RESIZE) {
return SELECT_NONE;
}
return SELECT_NONE;
}
/**
* The method that draws this label.
*
* @param drawToScreen
* Indicates whether the drawing happens on the screen, or
* elsewhere (eg, a file or a printer)
* @param g2
* The graphics context where the label has to be drawn.
*/
public final void jaxoDraw(VectorGraphics g2, boolean drawToScreen) {
if (textString.equals("")) {
return;
}
Font baseFont = getFont();
rtext.setColor(getColor());
rtext.setFont(getFont());
float x = (float) getX();
float y = (float) getY();
double theta = Math.toRadians(rotAngle);
g2.rotate(theta, x, y);
rtext.setRotation((int) rotAngle);
rtext.draw(g2, (int) x, (int) y);
g2.rotate(-theta, x, y);
FontRenderContext frc = g2.getFontRenderContext();
TextLayout layout = new TextLayout(textString, baseFont, frc);
Rectangle2D bb = layout.getBounds();
GeneralPath gp = new GeneralPath((Shape) bb);
AffineTransform at = AffineTransform.getRotateInstance(theta);
gp.transform(at);
bb = gp.getBounds();
double[] bbox = { bb.getMinX() + getX(), bb.getMinY() + getY(),
bb.getMaxX() + getX(), bb.getMaxY() + getY() + 5 };
setBoundingBox(bbox);
}
/**
* Draws the handles of this text object.
*
* @param g2
* The current graphics context.
*/
public final void drawHandles(VectorGraphics g2) {
int length = JaObject.LENGTH;
g2.setColor(JaxoColor.RED);
g2.setStroke(new BasicStroke(1.0f));
g2.drawRect(getX() - (length / 2), getY() - (length / 2), length,
length);
if (isMarked()) {
g2.setColor(JaxoColor.GRAYSCALE150);
g2.fillRect(getX() - (length / 2) + 1, getY() - (length / 2) + 1,
length - 1, length - 1);
}
}
/**
* The LaTeX command that is necessary to draw the given label using the
* axodraw.sty package. This is not used for label.
*
* @param scale
* A scale factor to translate Java coordinates to LaTeX
* coordinates.
* @param canvasDim
* The current dimension of the canvas.
* @return A string that appears as a comment in the Latex file, indicating
* that there is a postscript text here.
*/
public final String latexCommand(float scale, Dimension canvasDim) {
String command = "% There is a postscript text here!";
return command;
}
/**
* Rescales this label 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) {
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()));
}
/**
* Returns the line width of this label. This is not used for label.
*
* @return Returns "%", corresponding to an empty comment line in LaTeX.
*/
public final String latexWidth() {
String lineWidth = "%";
return lineWidth;
}
/**
* 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() {
if (availableFonts == null) {
availableFonts = JaxoStaticCanvasMethods.getSystemFonts();
}
JaxoPSTextOptionsPanel textop = new JaxoPSTextOptionsPanel(this,
availableFonts);
return textop.hasChanged();
}
/**
* Sets the string array of available fonts.
*
* @param fonts
* The string array of available fonts.
*/
public final void setAvailableFonts(String[] fonts) {
this.availableFonts = fonts;
}
public final void print() {
System.out.println("JaText object");
System.out.println(" - text: " + getText());
System.out.println(" - (x,y): " + Integer.toString(getX())
+ "," + Integer.toString(getY()));
System.out.println(" - NDC (x,y): " + Double.toString(getXndc())
+ "," + Double.toString(getYndc()));
}
}
Ads help maintain this website.