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
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
Graphics2D supports four rendering
context instantiated by a
consists of the following fields, whose values are controlled by
various accessor methods:
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
setPaint( )method. Note that the
Colorclass implements the
Paintinterface, so you can pass
setPaint( )if you want to use solid colors.
Graphics2Duses 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.
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.
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,
Graphics2Duses 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
scale( ), and
- 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
RenderingHintsclass) 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:
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.
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.
Combine the colors with the existing drawing surface using the current compositing rule.
The rendering hints are used throughout to control the rendering quality.
Get Learning Java 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.