The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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文本布局类。
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 当您使用Java2D API时,可以通过使用Font
. 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
方法返回可用字体系列的列表。
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机制:
TextLayout
class manages text layout, highlighting, and hit detection. TextLayout
类管理文本布局、突出显示和命中检测。TextLayout
handle the most common cases, including strings with mixed fonts, mixed languages, and bidirectional text.TextLayout
提供的功能处理最常见的情况,包括具有混合字体、混合语言和双向文本的字符串。GlyphVector
objects by using the Font
class and then rendering each GlyphVector
object through the Graphics2D
class. Font
类创建自己的GlyphVector
对象,然后通过Graphics2D
类渲染每个GlyphVector
对象。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.要了解有关渲染提示的详细信息,请参阅控制渲染质量课程。