Measuring Font Sizes | Heaton Research

Measuring Font Sizes

When you display a font in Java you must specify the x and y coordinates that the text will be displayed at. Coordinates are usually expressed in the form (x,y), where x is the horizontal position and y is the vertical position.

Position (0,0) is in the upper left-hand corner of the applet. As x is increased you move to the right, as y is increased you move down. Figure 1.3 hows how the applet is laid out in coordinates.

Figure 1.3 Applet coordinates for a 100 width by 200 height applet

It is also important to understand how Java draws fonts. Fonts are drawn from the bottom up. For example, if you drew a text at coordinates (0,0) you would not see your text displayed. This is because the baseline would be at the top of the applet, and the text would be displayed off the top of the applet. Figure 1.4shows how Java draws a font relative to the "baseline" that you specify in the call to "drawString".

Figure 1.4 How Java draws text relative to the baseline

To properly draw text you must know the exact height and width of text, in pixels. To do this you use the FontMetrics object. Lets look at an example that makes use of FontMetrics. We will now example a simple applet that centers text on its display. This program is shown in Listing 1.3.

Listing 1.3: Using FontMetrics (FontMetricsApplet.java)

import java.awt.*;
import java.applet.*;

public class FontMetricsApplet extends Applet
{
  public void paint(Graphics g)
  {
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.BLACK);

    // first set y to the first line that will be on-screen
    int y = fm.getHeight();
    // draw a line of text there
    g.drawString("Hello World",10,y);

    // center a line of text
    String str = "Hello World Centered";
    int x = (getWidth()/2)-(fm.stringWidth(str)/2);
    y+=fm.getHeight();// move down one line of text
    g.drawString(str,x,y);
  }
}

This applet will center text, as seen in Figure 1.5.

Figure 1.5: Centering text with an applet

The first step is to obtain a FontMetrics object. This is done by calling the "getFontMetrics" method. This must be done after you have set the font. The "Graphics" object will return a FontMetrics object for whatever the currently selected font is. If you select a new font, you must obtain a new FontMetrics object, for that font. The following line of code obtains the FontMetrics object.

FontMetrics fm = g.getFontMetrics();

Once the FontMetrics object has been obtained you can easily get the height and width of text. To get the height of any text call "fm.getHeight", to get the width call "fm.getWidth()". When you call getWidth you must pass in the string that you want the width of.

You can also find out the size of your applet’s box. Simply call the getWidth() and getHeight() methods.

The following code centers a line of text.

int x = (getWidth()/2)-(fm.stringWidth(str)/2);

The x coordinate is calculated by taking 1/2 of the width of the text from 1/2 the width of the applet box.

You can display text in any color supported by your computer. In the next section I will show you how to use the Java Color object.

Copyright 2005-2009 by Heaton Research, Inc.