Trail: 2D Graphics
Lesson: Working with Text and Fonts
Creating and Deriving Fonts
Creating and Deriving Fonts
You can display a text string with any font available on your system in any size and style that you choose. To determine what fonts are available on your system, you can call the GraphicsEnvironment.getAvailableFontFamilyNames method. This method returns an array of strings that contains the family names of the available fonts. Any of the strings, along with a size and style argument, can be used to create a new Font object. After creating a Font object, you can reset its font family name, size or style to create a custom font.

Example: FontSelection

The following applet allows you to change the font, size, and style of the displayed text.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

The complete code for this applet is in FontSelection.java.

The getAvailableFontFamilyNames method of GraphicsEnvironment returns the font family names of the fonts available on your system.

GraphicsEnvironment gEnv =
    GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = gEnv.getAvailableFontFamilyNames();
Vector vector = new Vector();
for ( int i = 1; i < envfonts.length; i++ ) {
   vector.addElement(envfonts[i]);
}
The initial Font object is created with style Font.PLAIN and size 10. The other available styles are ITALIC, BOLD and BOLD+ITALIC.
Font thisFont;
...

thisFont = new Font("Arial", Font.PLAIN, 10);
A new Font is created from the specified font name, style and size.
public void changeFont(String f, int st, String si){
  Integer newSize = new Integer(si);
  int size = newSize.intValue();
  thisFont = new Font(f, st, size);
  repaint();
}
To use the same font family but change one or both of the style and size attributes, you can call one of the deriveFont methods.

To control the font used to render text, you set the font attribute in the Graphics2D context before rendering. The font attribute is set by passing a Font object to the setFont method. In this example, the font attribute is set to the newly constructed Font object and then the string is drawn in the center of the Component using the specified font. In the paint method, the font attribute of the Graphics2D context is set to the new Font. The string is drawn in the middle of the component with the new font.

g2.setFont(thisFont);
String change = "Pick a font, size, and style to change me";
FontMetrics metrics = g2.getFontMetrics();
int width = metrics.stringWidth( change );
int height = metrics.getHeight();
g2.drawString( change, w/2-width/2, h/2-height/2 );

Note:  Due to bug # 4155852, FontSelection might not work properly for all font names returned from the call to getFontFamilyNames. The sample might not respond to changes in size or style and the text might not show up at all when some fontnames are chosen. In general, Courier and Helvetica work fine. In the meantime, check back periodically to see if these problems have been fixed.

Previous page: Working with Text and Fonts
Next page: Drawing Multiple Lines of Text