The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: 2D Graphics
Lesson: Overview of the Java 2D API

Text

When you need to display text, you can use one of the text-oriented components, such as the Swing How to Use Labels(in the 2D Graphics trail) or Using Text Components(in the 2D Graphics 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 hit testing and displaying international text.

If you just want to draw a static text string, you can render it directly through Graphics2D by 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, whereas 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 the collection of font families forms the set fonts available on the system.

When you're using the Java 2D API, you specify fonts by using an instance of Font. You can determine what fonts are available by calling the static method GraphicsEnvironment.getLocalGraphicsEnvironment and then querying the returned GraphicsEnvironment. The getAllFonts method returns an array that contains Font instances for all of the fonts available on the system; getAvailableFontFamilyNames returns 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. To get a list of the logical font names, call 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 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 Using Text Components(in the 2D Graphics 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 Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.