/* * label.java * * Created on February 27, 2004, 10:08 AM */ /** * * @author linyuan */ public class Label extends Selectable { String label; int x; int y; private Selectable labelBase; private java.awt.Color color = java.awt.Color.black; // The font to use for this label. private java.awt.Font labelFont = new java.awt.Font("TimesRoman",java.awt.Font.PLAIN,14); // Metrics for labelFont. private java.awt.FontMetrics labelFontMetrics = java.awt.Toolkit.getDefaultToolkit().getFontMetrics(labelFont); // Make sure labelFontMetrics change whenever labelFont changes. public final void setFont(java.awt.Font f) { labelFont = f; labelFontMetrics = java.awt.Toolkit.getDefaultToolkit().getFontMetrics(f); } public int getWidth(){ return labelFontMetrics.stringWidth(label); } public int getHeight(){ return labelFontMetrics.getAscent() + labelFontMetrics.getDescent(); } public final void moveLabel(int x,int y) { this.x = x; this.y = y - labelFontMetrics.getAscent(); } // Shift label by x,y pixels. public final void moveLabelRelative(int x,int y) { if (labelBase==null) moveLabel(x,y); else moveLabel(x+this.x,y+this.y); } public final void extendLabel(char c) { if (Character.isISOControl(c)) { return; } if (label==null) label = ""; label += c; } public final void shortenLabel() { if ((label!=null) && (label.length()>1)) { label = label.substring(0,label.length()-1); } else label = ""; } // Display the label. public final void draw(java.awt.Graphics g) { if (label==null) return; if ("".equals(label)) return; if(isSelected()) g.setColor(selectedColor); else g.setColor(color); g.setFont(labelFont); g.drawString(label,x,y+labelFontMetrics.getDescent()); } public final java.awt.Font getFont() { return labelFont; } /** Creates a new instance of label */ public Label() { } public Label(int x, int y, String s){ this.x=x; this.y=y; label=s; } }