Documentation

The Java™ Tutorials
Hide TOC
Controlling Rendering Quality控制渲染质量
Trail: 2D Graphics
Lesson: Advanced Topics in Java2D

Controlling Rendering Quality控制渲染质量

Use the Graphics2D class rendering hints attribute to specify whether you want objects to be rendered as quickly as possible or whether you prefer that the rendering quality be as high as possible.使用“Graphics2D类渲染提示”属性指定是希望尽快渲染对象,还是希望渲染质量尽可能高。

To set or change the rendering hints attribute in the Graphics2D context, construct a RenderingHints object and pass it into Graphics2D by using the setRenderingHints method.要在Graphics2D上下文中设置或更改“渲染提示”属性,请构造一个RenderingHints 对象,并使用SetRenderingHights方法将其传递到Graphics2D中。If you just want to set one hint, you can call Graphics2D setRenderingHint and specify the key-value pair for the hint you want to set.如果只想设置一个提示,可以调用Graphics2D setRenderingHint并为要设置的提示指定键值对。(The key-value pairs are defined in the RenderingHints class.)(键值对在RenderingHights类中定义。)

For example, to set a preference for antialiasing to be used if possible, you could use setRenderingHint:例如,若要在可能的情况下设置要使用的抗锯齿首选项,可以使用setRenderingHint

public void paint (graphics g){
    Graphics2D g2 = (Graphics2D)g;
    RenderingHints rh = new RenderingHints(
             RenderingHints.KEY_TEXT_ANTIALIASING,
             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHints(rh);
...
}

Note:注意: Not all platforms support modification of the rendering mode, so specifying rendering hints does not guarantee that they will be used.并非所有平台都支持修改渲染模式,因此指定渲染提示并不保证将使用它们。

RenderingHints supports the following types of hints:RenderingHints支持以下类型的提示:

Hint暗示 Key Values
Antialiasing抗锯齿 KEY_ANTIALIASING VALUE_ANTIALIAS_ON
VALUE_ANTIALIAS_OFF
VALUE_ANTIALIAS_DEFAULT
Alpha Interpolation阿尔法插值 KEY_ALPHA_INTERPOLATION VALUE_ALPHA_INTERPOLATION_QUALITY
VALUE_ALPHA_INTERPOLATION_SPEED
VALUE_ALPHA_INTERPOLATION_DEFAULT
Color Rendering颜色渲染 KEY_COLOR_RENDERING VALUE_COLOR_RENDER_QUALITY
VALUE_COLOR_RENDER_SPEED
VALUE_COLOR_RENDER_DEFAULT
Dithering抖动 KEY_DITHERING VALUE_DITHER_DISABLE
VALUE_DITHER_ENABLE
VALUE_DITHER_DEFAULT
Fractional Text Metrics分数文本度量 KEY_FRACTIONALMETRICS VALUE_FRACTIONALMETRICS_ON
VALUE_FRACTIONALMETRICS_OFF
VALUE_FRACTIONALMETRICS_DEFAULT
Image Interpolation图像插值 KEY_INTERPOLATION VALUE_INTERPOLATION_BICUBIC
VALUE_INTERPOLATION_BILINEAR
VALUE_INTERPOLATION_NEAREST_NEIGHBOR
Rendering渲染 KEY_RENDERING VALUE_RENDER_QUALITY
VALUE_RENDER_SPEED
VALUE_RENDER_DEFAULT
Stroke Normalization Control描边标准化控制 KEY_STROKE_CONTROL VALUE_STROKE_NORMALIZE
VALUE_STROKE_DEFAULT
VALUE_STROKE_PURE
Text Antialiasing文本抗锯齿 KEY_TEXT_ANTIALIASING VALUE_TEXT_ANTIALIAS_ON
VALUE_TEXT_ANTIALIAS_OFF
VALUE_TEXT_ANTIALIAS_DEFAULT
VALUE_TEXT_ANTIALIAS_GASP
VALUE_TEXT_ANTIALIAS_LCD_HRGB
VALUE_TEXT_ANTIALIAS_LCD_HBGR
VALUE_TEXT_ANTIALIAS_LCD_VRGB
VALUE_TEXT_ANTIALIAS_LCD_VBGR
LCD Text ContrastLCD文本对比度 KEY_TEXT_LCD_CONTRAST Values should be a positive integer in the range 100 to 250.值应为100到250之间的正整数。A lower value (such as 100) corresponds to higher contrast text when displaying dark text on a light background.当在浅色背景上显示深色文本时,较低的值(如100)对应较高的对比度文本。
A higher value (such as 200) corresponds to lower contrast text when displaying dark text on a light background.当在浅色背景上显示深色文本时,较高的值(例如200)对应于较低的对比度文本。
A typical useful value is in the narrow range 140-180. If no value is specified, a system or implementation default value will be applied.典型的有用值在140-180的窄范围内。如果未指定值,将应用系统或实现默认值。

When a hint is set to default, the platform rendering default is used.将提示设置为默认值时,将使用平台渲染默认值。


Previous page: Compositing Graphics
Next page: Constructing Complex Shapes from Geometry Primitives