Jump to content
  • Advertisement

AWT GUI Facade (7): Draw text



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:

public void setColor(Color color) {
    if (graphics == null)

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);

public void drawText(String text, int x, int y, int width, int height) {
    if (graphics == null)
    FontMetrics fm = graphics.getFontMetrics();
    graphics.clipRect(x, y, width, height);
    graphics.drawString(text, x, y+fm.getAscent());

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)
    for (int i=2*size;i>=4;i--) {
        Font font = new Font("Arial",Font.PLAIN,i);
        FontMetrics fm = graphics.getFontMetrics();
        if (fm.getHeight() < size) {

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...

Continue reading on https://learngameprog.com ...


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!