Graphics2D
class extends the
Graphics
class to provide more sophisticated
control over geometry, coordinate transformations, color management,
and text layout. This is the fundamental class for rendering
2-dimensional shapes, text and images on the Java(tm) platform.
Coordinate Spaces
All coordinates passed to aGraphics2D
object are specified
in a device-independent coordinate system called User Space, which is
used by applications. The Graphics2D
object contains
an AffineTransform
object as part of its rendering state
that defines how to convert coordinates from user space to
device-dependent coordinates in Device Space.
Coordinates in device space usually refer to individual device pixels
and are aligned on the infinitely thin gaps between these pixels.
Some Graphics2D
objects can be used to capture rendering
operations for storage into a graphics metafile for playback on a
concrete device of unknown physical resolution at a later time. Since
the resolution might not be known when the rendering operations are
captured, the Graphics2D Transform
is set up
to transform user coordinates to a virtual device space that
approximates the expected resolution of the target device. Further
transformations might need to be applied at playback time if the
estimate is incorrect.
Some of the operations performed by the rendering attribute objects
occur in the device space, but all Graphics2D
methods take
user space coordinates.
Every Graphics2D
object is associated with a target that
defines where rendering takes place. A
GraphicsConfiguration
object defines the characteristics
of the rendering target, such as pixel format and resolution.
The same rendering target is used throughout the life of a
Graphics2D
object.
When creating a Graphics2D
object, the
GraphicsConfiguration
specifies the default transform for
the target of the Graphics2D
(a
Component
or Image
). This default transform maps the
user space coordinate system to screen and printer device coordinates
such that the origin maps to the upper left hand corner of the
target region of the device with increasing X coordinates extending
to the right and increasing Y coordinates extending downward.
The scaling of the default transform is set to identity for those devices
that are close to 72 dpi, such as screen devices.
The scaling of the default transform is set to approximately 72 user
space coordinates per square inch for high resolution devices, such as
printers. For image buffers, the default transform is the
Identity
transform.
Rendering Process
The Rendering Process can be broken down into four phases that are controlled by theGraphics2D
rendering attributes.
The renderer can optimize many of these steps, either by caching the
results for future calls, by collapsing multiple virtual steps into
a single operation, or by recognizing various attributes as common
simple cases that can be eliminated by modifying other parts of the
operation.
The steps in the rendering process are:
- Determine what to render.
-
Constrain the rendering operation to the current
Clip
. TheClip
is specified by aShape
in user space and is controlled by the program using the various clip manipulation methods ofGraphics
andGraphics2D
. This user clip is transformed into device space by the currentTransform
and combined with the device clip, which is defined by the visibility of windows and device extents. The combination of the user clip and device clip defines the composite clip, which determines the final clipping region. The user clip is not modified by the rendering system to reflect the resulting composite clip. - Determine what colors to render.
-
Apply the colors to the destination drawing surface using the current
Composite
attribute in theGraphics2D
context.
The three types of rendering operations, along with details of each of their particular rendering processes are:
-
Shape
operations-
If the operation is a
draw(Shape)
operation, then thecreateStrokedShape
method on the currentStroke
attribute in theGraphics2D
context is used to construct a newShape
object that contains the outline of the specifiedShape
. -
The
Shape
is transformed from user space to device space using the currentTransform
in theGraphics2D
context. -
The outline of the
Shape
is extracted using thegetPathIterator
method ofShape
, which returns aPathIterator
object that iterates along the boundary of theShape
. -
If the
Graphics2D
object cannot handle the curved segments that thePathIterator
object returns then it can call the alternategetPathIterator
method ofShape
, which flattens theShape
. -
The current
Paint
in theGraphics2D
context is queried for aPaintContext
, which specifies the colors to render in device space.
-
If the operation is a
-
Text operations
-
The following steps are used to determine the set of glyphs required
to render the indicated
String
:-
If the argument is a
String
, then the currentFont
in theGraphics2D
context is asked to convert the Unicode characters in theString
into a set of glyphs for presentation with whatever basic layout and shaping algorithms the font implements. -
If the argument is an
AttributedCharacterIterator
, the iterator is asked to convert itself to aTextLayout
using its embedded font attributes. TheTextLayout
implements more sophisticated glyph layout algorithms that perform Unicode bi-directional layout adjustments automatically for multiple fonts of differing writing directions. -
If the argument is a
GlyphVector
, then theGlyphVector
object already contains the appropriate font-specific glyph codes with explicit coordinates for the position of each glyph.
-
If the argument is a
-
The current
Font
is queried to obtain outlines for the indicated glyphs. These outlines are treated as shapes in user space relative to the position of each glyph that was determined in step 1. -
The character outlines are filled as indicated above
under
Shape
operations. -
The current
Paint
is queried for aPaintContext
, which specifies the colors to render in device space.
-
The following steps are used to determine the set of glyphs required
to render the indicated
-
Image
Operations-
The region of interest is defined by the bounding box of the source
Image
. This bounding box is specified in Image Space, which is theImage
object's local coordinate system. -
If an
AffineTransform
is passed todrawImage(Image, AffineTransform, ImageObserver)
, theAffineTransform
is used to transform the bounding box from image space to user space. If noAffineTransform
is supplied, the bounding box is treated as if it is already in user space. -
The bounding box of the source
Image
is transformed from user space into device space using the currentTransform
. Note that the result of transforming the bounding box does not necessarily result in a rectangular region in device space. -
The
Image
object determines what colors to render, sampled according to the source to destination coordinate mapping specified by the currentTransform
and the optional image transform.
-
The region of interest is defined by the bounding box of the source
Default Rendering Attributes
The default values for theGraphics2D
rendering attributes are:
Paint
- The color of the
Component
. Font
- The
Font
of theComponent
. Stroke
- A square pen with a linewidth of 1, no dashing, miter segment joins and square end caps.
Transform
- The
getDefaultTransform
for theGraphicsConfiguration
of theComponent
. Composite
- The