Graphic libraries usually provide methods to draw text on the screen. These handy methods are often quite slow to run because they recompute many parameters at each call. To save computational time, the flyweight pattern can be used to provide text parameters with low memory and cpu usage.
Facade design
As usual, I start by adding new methods to the GUI Facade. There are many possibilities, here is an example that allows the drawing of messages of any size and color (NB: I only show new methods, I assume that the ones from the previous posts are still there):
- The setColor() method sets the text color. It uses java.awt.Color to defines the colors. I could create my own Color class, but it won’t lead to significant changes, and java.awt.Color is always in the Java standard library;
- The setTextSize() method sets the text height in pixels;
- The getTextMetrics() method computes the bounding box of a text to print. It also uses a class from AWT, namely java.awt.Dimension;
- The drawText() method draws text at coordinates (x,y). It also clip the text if it is too large. The clipping rectangle is defined by (x,y) and (width,height).
AWT Implementation
Implementing this facade with AWT is easy for setColor(), getTextMetrics() and drawText(). This is only a wrapping of existing methods:
@Override
public void setColor(Color color) {
if (graphics == null)
return;
graphics.setColor(color);
}
@Override
public Dimension getTextMetrics(String text) {
if (graphics == null)
return new Dimension(0,0);
FontMetrics fm = graphics.getFontMetrics();
int textWidth = fm.stringWidth(text);
int textHeight = fm.getHeight();
return new Dimension(textWidth,textHeight);
}
@Override
public void drawText(String text, int x, int y, int width, int height) {
if (graphics == null)
return;
FontMetrics fm = graphics.getFontMetrics();
graphics.clipRect(x, y, width, height);
graphics.drawString(text, x, y+fm.getAscent());
graphics.setClip(null);
}
For the setTextSize() method, there is no existing method in AWT. A solution is to search for the font that has the required text height in pixels:
public void setTextSize(int size) {
if (graphics == null)
return;
for (int i=2*size;i>=4;i--) {
Font font = new Font("Arial",Font.PLAIN,i);
graphics.setFont(font);
FontMetrics fm = graphics.getFontMetrics();
if (fm.getHeight() < size) {
break;
}
}
}
This method is time-consuming and can be called many times at each frame rendering. Multiplied by the number of frames per second (usually 60), this can lead to a lot of unuseful computations since most of these calls will return the same values!
A first approach consists of manually caching the parameters (in this example the AWT font instance). This saves computation time but requires additional work for the user and the implementer of the facade.
A more efficient approach is based on the Flyweight pattern.
Flyweight Pattern
The Flyweight pattern...