package simple_uml.view.canvas; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; /** * Represents a single line of text. Line wrapping is currently not supported. * * @author Eric Liebowitz * @version 02jul11 */ public class Text extends SumlShape { /** The string this object will display */ private String s; /** * Creates Text which'll display the specified string at the given origin. * * @param s The string you want displayed * @param x The x-coord of the upper-left corner of where you want the text's * bounding box to be * @param y The y-coord of the upper-left corner of where you want the text's * bounding box to be */ public Text (String s, int x, int y) { this.s = s; this.origin = new Point(x, y); } /** * Draws the text object, with the upper-left corner of the text's bounding * box (and thus, its first letter) at the point specified in the * constructor. * * @param g2d Graphics2D object to be used to draw */ public void draw (Graphics2D g2d) { FontMetrics fm = g2d.getFontMetrics(SumlCanvas.MONO_FONT); /* * I have to adjust the "origin", as the "baseline" is what gets drawn at * origin, allowing text to go above said origin. To fix this, I shift the * baseline down as much as any character might surpass it. */ g2d.drawString(this.s, (int)origin.getX(), (int)(origin.getY() + fm.getAscent())); this.bounds = new Rectangle(fm.stringWidth(this.s), fm.getHeight()); } }