Previous | Next | Trail Map | 2D Graphics | Overview of the Java 2D API

Text

Generally, when you need to display text you can use one of the text-oriented components, such as the Swing label(in the Creating a User Interface trail) or text(in the Creating a User Interface trail) components. When you use a text component, a lot of the work is done for you--for example, JTextComponent objects provide built-in support for things like hit testing and displaying international text.

If you just want to draw a static text string, you can render it directly through Graphics2D using the drawString method. To specify the font, you use the Graphics2D setFont method.

If you want to implement your own text editing routines or need more control over the layout of the text than the text components provide, you can use the Java 2D text layout classes in java.awt.font .

Fonts

The shapes that a font uses to represent the characters in a string are called glyphs . A particular character or combination of characters might be represented as one or more glyphs. For example, á might be represented by two glyphs, while the ligature fi might be represented by a single glyph.

A font can be thought of as a collection of glyphs. A single font might have many faces , such as heavy, medium, oblique, gothic, and regular. All of the faces in a font have similar typographic features and can be recognized as members of the same family . In other words, a collection of glyphs with a particular style form a font face , a collection of font faces forms a font family , and a collection of font families forms the set fonts available on the system.

When you're using the Java 2D API, you specify fonts using an instance of Font . You can determine what fonts are available by calling the static method GraphicsEnvironment.getLocalGraphicsEnvironment and querying the GraphicsEnvironment . The getAllFonts method returns an array that contains Font instances for all of the fonts available on the system; getAvailableFontFamilyNames r eturns a list of the available font families.

The GraphicsEnvironment also describes the collection of platform rendering devices such as screens and printers that a Java program can use. This information is used when the system performs the conversion from user space to device space during rendering.


Note: In JDK 1.1, fonts were described by logical names that mapped onto different font faces depending on the fonts that were available on a particular platform. For backward compatibility, Graphics2D also supports the specification of fonts by logical name. You can get a list of the logical font names by calling java.awt.Toolkit.getFontList.

Text Layout

Before text can be displayed, it must be laid out so that the characters are represented by the appropriate glyphs and the glyphs are in the proper positions. If you are using Swing, you can let JLabel or JTextComponent manage text layout for you. JTextComponent supports bidirectional text and is designed to handle the needs of most international applications. For more information about using Swing text components, see How to Use Text Components(in the Creating a User Interface trail).

If you are not using a Swing text component to automatically display text, you can use one of two Java 2D mechanisms for managing text layout:


Previous | Next | Trail Map | 2D Graphics | Overview of the Java 2D API