Documentation

The Java™ Tutorials
Hide TOC
Text文本
Trail: 2D Graphics
Lesson: Overview of the Java 2D API Concepts

Text文本

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.Java 2D API具有各种文本呈现功能,包括用于呈现字符串的方法以及用于设置字体属性和执行文本布局的整个类。

If you just want to draw a static text string, the most direct way to render it directly through the Graphics class by using the drawString method. 如果只想绘制静态文本字符串,最直接的方法是使用drawString方法直接通过Graphics类进行渲染。To specify the font, you use the setFont method of the Graphics class.要指定字体,请使用Graphics类的setFont方法。

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.font package.如果您想要实现自己的文本编辑例程,或者需要比文本组件提供的更多的文本布局控制,那么可以使用java.awt.font包中的Java 2D文本布局类。

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.例如,á可能由两个标志符号表示,而连字fi可能由一个标志符号表示。

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. 当您使用Java2D API时,可以通过使用Font实例来指定字体。You can determine what fonts are available by calling the static method GraphicsEnvironment.getLocalGraphicsEnvironment and then querying the returned GraphicsEnvironment. 通过调用静态方法GraphicsEnvironment.getLocalGraphicsEnvironment,然后查询返回的GraphicsEnvironment,可以确定可用的字体。The getAllFonts method returns an array that contains Font instances for all of the fonts available on the system. getAllFonts方法返回一个数组,该数组包含系统上所有可用字体的字体实例。The getAvailableFontFamilyNames method returns a list of the available font families.getAvailableFontFamilyNames方法返回可用字体系列的列表。

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. 在显示文本之前,必须对其进行布局,以便在适当的位置用适当的标志符号表示字符。The following are two Java 2D mechanisms for managing text layout:以下是用于管理文本布局的两种Java 2D机制:

Rendering Hints for Text文本的呈现提示

The Java 2D API enables you to control the quality of shapes and text rendering by using rendering hints. Java2D API使您能够通过使用渲染提示来控制形状和文本渲染的质量。Rendering hints are encapsulated by the java.awt.RenderingHints class.呈现提示由java.awt.RenderingHints类封装。

As applied to text, this capability is used for antialiasing (which is also known as an smooth edges). 应用于文本时,此功能用于抗锯齿(也称为平滑边)。For example, the KEY_TEXT_ANTIALIASING hint enables you to control the antialiasing of text separately from the antialiasing of other shapes. 例如,KEY_TEXT_ANTIALIASING提示使您能够分别控制文本的抗锯齿和其他形状的抗锯齿。To learn more about rendering hints see the Controlling Rendering Quality lesson.要了解有关渲染提示的详细信息,请参阅控制渲染质量课程。


Previous page: Geometric Primitives
Next page: Images