*/
private Font font;
+ private TextLayout layout;
+
/**
* Default constructor.
* @param text the text content.
*/
public void setText(String text) {
this.text = text;
+ this.layout = null;
}
/**
*/
public void setFont(Font font) {
this.font = font;
+ this.layout = null;
}
/**
*/
public void setTextAntiAliased(boolean textAntiAliased) {
this.textAntiAliased = textAntiAliased;
+ this.layout = null;
}
/**
*/
public void setTextUseFractionalMetrics(boolean textUseFractionalMetrics) {
this.textUseFractionalMetrics = textUseFractionalMetrics;
+ this.layout = null;
}
/**
);
}
+ public TextLayout getLayout() {
+ if (layout == null) {
+ FontRenderContext frc = new FontRenderContext(null, isTextAntiAliased(), isTextUseFractionalMetrics());
+ layout = new TextLayout(getText(), getFont(), frc);
+ }
+
+ return layout;
+ }
+
/**
* Return the dimension in pixel of the text entity.
* @return the dimension in pixel of the text entity.
*/
public Dimension getSize() {
if (isValid()) {
- FontRenderContext frc = new FontRenderContext(null, isTextAntiAliased(), isTextUseFractionalMetrics());
- TextLayout textLayout = new TextLayout(getText(), getFont(), frc);
+ TextLayout textLayout = getLayout();
Dimension dimension = new Dimension();
Rectangle2D r = textLayout.getBounds();
/* +1 added to fix rendering of ticks labels, a pixel row/column was missing */
- dimension.setSize(r.getWidth() + 2, r.getHeight() + 1);
+ dimension.setSize(r.getWidth() + 2, textLayout.getAscent() + textLayout.getDescent() + 1);
return dimension;
} else {
return new Dimension(0, 0);