- private static final long serialVersionUID = 8508750881358776559L;\r
-\r
- String text = null;\r
- Font font = null;\r
- Color color = Color.BLACK;\r
- \r
- transient double sx, sy, tw, th, tx, ty;\r
-\r
- @SyncField("font")\r
- public void setFont(Font font) {\r
- this.font = font;\r
- }\r
- \r
- @SyncField("text")\r
- public void setText(String text) {\r
- this.text = text;\r
- }\r
- \r
- @SyncField("color")\r
- public void setColor(Color color) {\r
- this.color = color;\r
- }\r
- \r
- public void layout() {\r
- GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);\r
- Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();\r
- tw = tr.getWidth();\r
- th = tr.getHeight();\r
- sx = getWidth() / tw;\r
- sy = getHeight() / th;\r
- tx = -tr.getX();\r
- ty = -tr.getY();\r
- \r
- // Fix horiz scale param sx\r
- if (sx<1.0) {\r
- // Text is too wide\r
- // Scale-down to fit rectangle width\r
- tw *= sx;\r
- th *= sx;\r
- sy *= sx;\r
- tx *= sx;\r
- ty *= sx;\r
- } else {\r
- // Text is ok horizontally. No need to scale.\r
- sx = 1.0;\r
- }\r
- \r
- // Fix vert scale param sy\r
- if (sy<1.0) {\r
- // Text is too tall\r
- // Scale-down to fit rectangle height\r
- tw *= sy;\r
- th *= sy;\r
- sx *= sy;\r
- tx *= sy;\r
- ty *= sy;\r
- } else {\r
- // Text is ok vertically. No need to scale.\r
- sy = 1.0;\r
- }\r
- \r
- tx += (bounds.getWidth() - tw) / 2;\r
- ty += (bounds.getHeight() - th) / 2; \r
- }\r
-\r
- @Override\r
- protected void doRender(Graphics2D g) {\r
- if (text == null || bounds == null || font == null || color == null)\r
- return;\r
-\r
- g.setFont(font);\r
- g.setColor(color);\r
- \r
- AffineTransform at = g.getTransform();\r
- \r
- g.translate( bounds.getX(), bounds.getY() );\r
- g.translate(tx, ty);\r
- g.scale(sx, sy);\r
-\r
- g.drawString(text, 0, 0);\r
- g.setTransform( at );\r
- } \r
+ private static final long serialVersionUID = 8508750881358776559L;
+
+ String text = null;
+ Font font = null;
+ Color color = Color.BLACK;
+
+ transient double sx, sy, tw, th, tx, ty;
+
+ @SyncField("font")
+ public void setFont(Font font) {
+ this.font = font;
+ }
+
+ @SyncField("text")
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ @SyncField("color")
+ public void setColor(Color color) {
+ this.color = color;
+ }
+
+ public void layout() {
+ GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);
+ Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();
+ tw = tr.getWidth();
+ th = tr.getHeight();
+ sx = getWidth() / tw;
+ sy = getHeight() / th;
+ tx = -tr.getX();
+ ty = -tr.getY();
+
+ // Fix horiz scale param sx
+ if (sx<1.0) {
+ // Text is too wide
+ // Scale-down to fit rectangle width
+ tw *= sx;
+ th *= sx;
+ sy *= sx;
+ tx *= sx;
+ ty *= sx;
+ } else {
+ // Text is ok horizontally. No need to scale.
+ sx = 1.0;
+ }
+
+ // Fix vert scale param sy
+ if (sy<1.0) {
+ // Text is too tall
+ // Scale-down to fit rectangle height
+ tw *= sy;
+ th *= sy;
+ sx *= sy;
+ tx *= sy;
+ ty *= sy;
+ } else {
+ // Text is ok vertically. No need to scale.
+ sy = 1.0;
+ }
+
+ tx += (bounds.getWidth() - tw) / 2;
+ ty += (bounds.getHeight() - th) / 2;
+ }
+
+ @Override
+ protected void doRender(Graphics2D g) {
+ if (text == null || bounds == null || font == null || color == null)
+ return;
+
+ g.setFont(font);
+ g.setColor(color);
+
+ AffineTransform at = g.getTransform();
+
+ g.translate( bounds.getX(), bounds.getY() );
+ g.translate(tx, ty);
+ g.scale(sx, sy);
+
+ g.drawString(text, 0, 0);
+ g.setTransform( at );
+ }