The JavaTM Tutorial

| 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 or Using Text Components 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.

> 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.

> - If you want to implement your own text-editing routines, you can use the `TextLayout` class to manage text layout, highlighting, and hit detection. The facilities provided by `TextLayout` handle most common cases, including strings with mixed fonts, mixed languages, and bidirectional text. For more information about `TextLayout`, see Managing Text Layout in the Java 2D Programmer's Guide.
> - If you want total control over how text is shaped and positioned, you can construct your own `GlyphVector` objects by using `Font` and then render each `GlyphVector` through `Graphics2D`. For more information about implementing your own text layout mechanisms, Implementing a Custom Text Layout Mechanism in the Java 2D Programmer's Guide.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

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