The Rendering Pipeline

One of the strengths of the 2D API is that shapes, text, and images are manipulated in many of the same ways. In this section, we’ll describe what happens to shapes, text, and images after you give them to a Graphics2D. Rendering is the process of taking some collection of shapes, text, and images and figuring out how to represent them by coloring pixels on a screen or printer. Graphics2D supports four rendering operations:

  1. Draw the outline of a shape, with the draw( ) method.

  2. Fill the interior of a shape, with the fill( ) method.

  3. Draw some text, with the drawString( ) method.

  4. Draw an image, with any of the many forms of the drawImage( ) method.

The graphics context instantiated by a Graphics2D object consists of the following fields, whose values are controlled by various accessor methods:

paint

The current paint (an object of type java.awt.Paint) determines what colors will be used to fill a shape. This affects shape outlines and text, as well. You can change the current paint using Graphics2D ’s setPaint( ) method. Note that the Color class implements the Paint interface, so you can pass Colors to setPaint( ) if you want to use solid colors.

stroke

Graphics2D uses the current stroke for shapes that are passed to its draw( ) method. The outline of the shape is represented by another shape that is determined by the current stroke. The resulting shape (the stroked outline) is then filled. Suppose you wanted to draw the outline of a circle. If the current stroke called for a solid line, the stroked outline of a circle would look like a washer or ring. You can set the current stroke using setStroke( ) . The 2D API comes with a handy class, java.awt.BasicStroke , that implements different line widths, end styles, join styles, and dashing.

font

Text is rendered by creating a shape that represents the characters to be drawn. The current font determines what shapes are created for a given set of characters. The resulting text shape is then filled. The current font is set using setFont( ). The 2D API gives applications access to all the TrueType and PostScript Type 1 fonts that are installed on your machine.

transformation

Shapes, text, and images are geometrically transformed before they are rendered. This means that they may be moved, rotated, and stretched. Graphics2D’s transformation converts coordinates from user space to device space. By default, Graphics2D uses a transformation that maps 72 units in user space to one inch on the output device. If you draw a line from point 0, 0 to point 72, 0 using the default transformation, it will be one inch long, regardless of whether it is drawn on your monitor or your printer. The current transformation can be modified using the translate( ) , rotate( ), scale( ), and shear( ) methods.

compositing rule

A compositing rule is used to determine how the colors of a primitive should be combined with existing colors on the Graphics2D’s drawing surface. This attribute is set using setComposite( ) , which accepts an instance of java.awt.AlphaComposite. Compositing allows you to make parts of a drawing or image completely or partially transparent, or to combine them in other interesting ways.

clipping shape

All rendering operations are limited to the interior of the clipping shape . No pixels outside this shape will be modified. By default, the clipping shape allows rendering on the entire drawing surface (usually, the rectangular area of a Component). However, you can further limit this using any simple or complex shape, including text shapes.

rendering hints

There are different techniques that can be used to render graphics primitives. Usually these represent a tradeoff between rendering speed and visual quality or vice versa. Rendering hints (constants defined in the RenderingHints class) specify which techniques to use.

Graphics primitives (shapes, text, and images) pass through the rendering engine in a series of operations called the rendering pipeline . Let’s walk through the pipeline. It can be reduced to four steps, where the first step depends heavily on the rendering operation:

  1. Transform the shape. For shapes that will be filled, the shape is simply transformed using the Graphics2D’s current transformation. For shapes whose outlines are drawn using draw( ), the current stroke is used to stroke the shape’s outline. Then the stroked outline is transformed, just like any other filled shape. Text is displayed by mapping characters to shapes using the current font. The resulting text shapes are transformed, just like any other filled shape. For images, the outline of the image is transformed using the current transformation.

  2. Determine the colors to be used. For a filled shape, the current paint object determines what colors should be used to fill the shape. For an image, the colors are taken from the image itself.

  3. Combine the colors with the existing drawing surface using the current compositing rule.

  4. Clip the results using the current clipping shape .

The rendering hints are used throughout to control the rendering quality.

Get Learning Java now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.