5.4. Rendering Hints

The 2D API allows you to control the quality of its rendering through the use of rendering hints. They are called hints because they don't directly control the rendering engine; they merely suggest your preferences. The rendering engine can follow the hints or ignore them as it wishes. Rendering hints are encapsulated by the java.awt.RenderingHints class.

The rendering engine, Graphics2D, usually knows more than one way of doing things. If it's drawing a shape, it can use antialiasing, which looks good, or it can draw the shape with aliasing, which is fast. Similar tradeoffs of quality for speed are available in other parts of the rendering pipeline. Rendering hints give you a set of knobs that you can fiddle with to adjust how Graphics2D performs.

Rendering hints are used in one other place: image processing. (See Chapter 10, for the full story.) The image processing operators all accept a RenderingHints object in their constructors. This gives the operator suggestions about whether to give preference to processing speed or quality.

Rendering hints are specified using a key and value scheme. You tell Graphics2D that you want a certain key, like KEY_ANTIALIASING, to have a certain value, such as VALUE_ANTIALIAS_ON. The Graphics2D will then use antialiasing in its rendering, if it can. Keys are represented by constants in the RenderingHints class, which are all instances of RenderingHints.Key. The values are also represented by constants in the RenderingHints ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.