Trail: 2D Graphics
Lesson: Working with Text APIs
Selecting a Font
Home Page > 2D Graphics > Working with Text APIs
Selecting a Font

Java 2D™ defines the following five logical font families:

These fonts are available on any Java platform and can be thought of as aliases for some underlying font that has the properties implied by its name. A Serif font is a font similar to Times New Roman, which is commonly used in print. A Sans Serif font is more typical for onscreen use.

These fonts can be customized for the locale of the user. In addition these fonts support the widest range of code points (unicode characters).

Apart from the family, fonts have other attributes, the most important of which are style and size. Styles are Bold and Italic.

The default Java 2D font is 12 pt Dialog. This font is a typical point size for reading text on a normal 72–120 DPI display device. An application can create an instance of this font directly by specifying the following:

Font font = new Font("Dialog", Font.PLAIN, 12);

In addition to the logical fonts, Java software provides access to other fonts that are installed on your system. The names of all available font families can be found by calling the following:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();

The FontSelector sample program (available in FontSelector.java) illustrates how to locate and select these fonts. You can use this example to view how Sans Serif appears on your system. These other fonts are called physical fonts.


Note: Applications should not assume that any particular physical font is present. However, the logical fonts are a safe choice because they are always present.


Note: If you don't see the applet running above, you need to install release 6 of the JDK.

Sometimes, an application cannot depend on a font being installed on the system, usually because the font is a custom font that is not otherwise available. In this case, the application must include the font. This lesson shows how to obtain a TrueType font, the most commonly used font type on modern operating systems, to a java.awt.Font object.

You can use either of these methods:

Font java.awt.Font.createFont(int fontFormat, InputStream in);
Font java.awt.Font.createFont(int fontFormat, File fontFile);
To identify a TrueType font, fontFormat must be the constant Font.TRUETYPE_FONT.
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));

Accessing the font directly from a file must be more convenient for some cases. However, an InputStream might be needed if your code is unable to access file system resources, or if the font is packaged in a Java Archive (JAR) file along with the rest of the application or applet.

The returned Font instance can then be used with the Font.deriveFont(..) methods to derive a version that is the required size. For example:

try {
     /* Returned font is of pt size 1 */
     Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));

     /* derive and return a 12 pt version : need to use float otherwise
      * it would be interpreted as style
      */
     return font.deriveFont(12f);

} catch (IOException ioe);
} catch (FontFormatException ffe);
}

It is important to use deriveFont() because fonts which are created by application are not part of the set of fonts known to the underlying font system. Because deriveFont works from the original created font it does not have this limitation.

The solution for this problem is to register the created font with the graphics environment. For example:

try {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
} catch (IOException ioe);
} catch (FontFormatException ffe);
}
After this step is done, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.
Previous page: Working with Text APIs
Next page: Measuring Text