11 static final int MAX_LINES = 50;
13 private String[] theText =
new String[MAX_LINES];
14 private static String name =
"SansSerif";
15 private static int style = Font.PLAIN;
16 private static int size = 18;
17 private static Font font;
18 private double previousMag;
19 private boolean firstChar =
true;
20 private boolean firstMouseUp =
true;
21 private int cline = 0;
25 double mag = (imp!=null)?imp.getCanvas().getMagnification():1.0;
30 theText[0] =
"Type, then";
32 if (previousRoi!=null && (previousRoi instanceof
TextRoi)) {
41 if (!(c>=
' ' || c==
'\b' || c==
'\n'))
return;
44 theText[cline] =
new String(
"");
45 for (
int i=1; i<MAX_LINES; i++)
50 if (theText[cline].length()>0)
51 theText[cline] = theText[cline].substring(0, theText[cline].length()-1);
53 theText[cline] = null;
56 imp.
draw(clipX, clipY, clipWidth, clipHeight);
59 }
else if ((
int)c==
'\n') {
61 if (cline<(MAX_LINES-1)) cline++;
66 theText[cline] +=
new String(chr);
69 imp.
draw(clipX, clipY, clipWidth, clipHeight);
75 Font getCurrentFont() {
76 double mag = ic.getMagnification();
77 if (font==null || mag!=previousMag) {
78 font =
new Font(name, style, (
int)(size*mag));
89 Font font =
new Font(name, style, size);
93 int fontHeight = metrics.getHeight();
94 int descent = metrics.getDescent();
97 while (i<MAX_LINES && theText[i]!=null) {
107 g.setColor(ROIColor);
108 double mag = ic.getMagnification();
111 int swidth = (int)(width*mag);
112 int sheight = (int)(height*mag);
114 Java2.setAntialiasedText(g,
true);
117 Font font = getCurrentFont();
118 FontMetrics metrics = g.getFontMetrics(font);
119 int fontHeight = metrics.getHeight();
120 int descent = metrics.getDescent();
122 Rectangle r = g.getClipBounds();
123 g.setClip(sx, sy, swidth, sheight);
125 while (i<MAX_LINES && theText[i]!=null) {
126 g.drawString(theText[i], sx, sy+fontHeight-descent);
130 if (r!=null) g.setClip(r.x, r.y, r.width, r.height);
157 public static void setFont(String fontName,
int fontSize,
int fontStyle) {
164 Roi roi = imp.getRoi();
171 protected void handleMouseUp(
int screenX,
int screenY) {
172 super.handleMouseUp(screenX, screenY);
175 firstMouseUp =
false;
177 if (width<5 || height<5)
187 double mag = ic.getMagnification();
188 Font font = getCurrentFont();
189 Graphics g = ic.getGraphics();
191 Java2.setAntialiasedText(g,
true);
192 FontMetrics metrics = g.getFontMetrics(font);
193 int fontHeight = (int)(metrics.getHeight()/mag);
194 int descent = metrics.getDescent();
201 while (i<MAX_LINES && theText[i]!=null) {
203 int w = (int)(stringWidth(theText[i],metrics,g)/mag);
212 height = nLines*fontHeight+2;
218 imp.
draw(clipX, clipY, clipWidth, clipHeight);
221 int stringWidth(String s, FontMetrics metrics, Graphics g) {
223 return Java2.getStringWidth(s, metrics, g);
225 return metrics.stringWidth(s);