The Java 2D™ API has various text rendering capabilities including methods for rendering strings and entire classes for setting font attributes and performing text layout.If you just want to draw a static text string, the most direct way to render it directly through the
Graphicsclass by using thedrawStringmethod. To specify the font, you use thesetFontmethod of theGraphicsclass.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 the
java.awt.fontpackage.
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 italic 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. The collection of font families forms the set of fonts that are available on the system.
When you are 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 methodGraphicsEnvironment.getLocalGraphicsEnvironmentand then querying the returnedGraphicsEnvironment. ThegetAllFontsmethod returns an array that containsFontinstances for all of the fonts available on the system. ThegetAvailableFontFamilyNamesmethod returns a list of the available font families.
Before text can be displayed, it must be laid out so that the characters are represented by the appropriate glyphs in the proper positions. The following are two Java 2D mechanisms for managing text layout:
- The
TextLayoutclass manages text layout, highlighting, and hit detection. The facilities provided byTextLayouthandle the most common cases, including strings with mixed fonts, mixed languages, and bidirectional text. .- You can create the own
GlyphVectorobjects by using theFontclass and then rendering eachGlyphVectorobject through theGraphics2Dclass. Thus, you can completely control how text is shaped and positioned. .
The Java 2D API enables you to control the quality of shapes and text rendering by using rendering hints. Rendering hints are encapsulated by thejava.awt.RenderingHintsclass.As applied to text, this capability is used for antialiasing (which is also known as an smooth edges). For example, the
KEY_TEXT_ANTIALIASINGhint enables you to control the antialiasing of text separately from the antialiasing of other shapes. To learn more about rendering hints see the Controlling Rendering Quality lesson.