The Java Tutorials have been written for 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.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
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.
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. 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. (The key-value pairs are defined in the RenderingHints class.)
For example, to set a preference for antialiasing to be used if possible, you could use 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);
...
}
RenderingHints supports the following types of hints:
| Hint | Key | Values |
|---|---|---|
| Antialiasing | KEY_ANTIALIASING |
VALUE_ANTIALIAS_ONVALUE_ANTIALIAS_OFFVALUE_ANTIALIAS_DEFAULT |
| Alpha Interpolation | KEY_ALPHA_INTERPOLATION |
VALUE_ALPHA_INTERPOLATION_QUALITYVALUE_ALPHA_INTERPOLATION_SPEEDVALUE_ALPHA_INTERPOLATION_DEFAULT |
| Color Rendering | KEY_COLOR_RENDERING |
VALUE_COLOR_RENDER_QUALITYVALUE_COLOR_RENDER_SPEEDVALUE_COLOR_RENDER_DEFAULT |
| Dithering | KEY_DITHERING |
VALUE_DITHER_DISABLEVALUE_DITHER_ENABLEVALUE_DITHER_DEFAULT |
| Fractional Text Metrics | KEY_FRACTIONALMETRICS |
VALUE_FRACTIONALMETRICS_ONVALUE_FRACTIONALMETRICS_OFFVALUE_FRACTIONALMETRICS_DEFAULT |
| Image Interpolation | KEY_INTERPOLATION |
VALUE_INTERPOLATION_BICUBICVALUE_INTERPOLATION_BILINEARVALUE_INTERPOLATION_NEAREST_NEIGHBOR |
| Rendering | KEY_RENDERING |
VALUE_RENDER_QUALITYVALUE_RENDER_SPEEDVALUE_RENDER_DEFAULT |
| Stroke Normalization Control | KEY_STROKE_CONTROL |
VALUE_STROKE_NORMALIZEVALUE_STROKE_DEFAULTVALUE_STROKE_PURE |
| Text Antialiasing | KEY_TEXT_ANTIALIASING |
VALUE_TEXT_ANTIALIAS_ONVALUE_TEXT_ANTIALIAS_OFFVALUE_TEXT_ANTIALIAS_DEFAULTVALUE_TEXT_ANTIALIAS_GASPVALUE_TEXT_ANTIALIAS_LCD_HRGBVALUE_TEXT_ANTIALIAS_LCD_HBGRVALUE_TEXT_ANTIALIAS_LCD_VRGBVALUE_TEXT_ANTIALIAS_LCD_VBGR |
| LCD Text Contrast | KEY_TEXT_LCD_CONTRAST |
Values should be a positive integer in the range 100 to 250. A lower value (such as 100) corresponds to higher contrast text when displaying dark text on a light background. A higher value (such as 200) corresponds to lower contrast text when displaying dark text on a light background. 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. |
When a hint is set to default, the platform rendering default is used.