CONTENTS | PREV | NEXT Java 2D API


2.1 Concepts

To render a graphic object using the Java 2D API, you set up the Graphics2D context, define the object you want to render, and then call one of the Graphics2D rendering methods.

The Graphics2D context is a collection of state attributes that are applied to the object when it is rendered. With these state attributes, you can:

Graphics2D defines several methods for adding and changing attributes in the graphics context. Most of these methods take an attribute object that represents a particular attribute, such as a Paint or Stroke object. The Graphics2D context holds references to these attribute objects, they are not cloned. If you alter an attribute object that is part of the Graphics2D context, you need to call the appropriate set method to notify the context. Modifying an attribute object during a rendering operation will result in unpredictable and possibly unstable behavior.


2.1.1 Rendering Process

When a graphic object is rendered, the geometry, image, and attribute information is combined to calculate which pixel values must be changed on the display.

The rendering process for a geometric shape, including a glyph obtained from a font, can be thought of as a series of steps:

  1. If the shape is to be stroked, the Stroke attribute in the Graphics2D context converts the shape into a new shape that accounts for the geometry of the stroked path. The methods that use stroking are draw, drawArc, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, and drawRoundRect.
  2. The coordinates of the shape's path are transformed from user space into device space according to the transform attribute in the Graphics2D context.
  3. The shape's path is clipped using the clip attribute in the Graphics2D context.
  4. The remaining shape, if any, is filled using the Paint and Composite attributes in the Graphics2D context.
Rendering text is similar to rendering shapes, since the glyphs for the text are themselves shapes. The only difference is that the Java 2D API must first determine what font to apply to the text before rendering the glyphs.

Images are handled differently:

  1. The bounding box of the image is transformed into device space using the Transform in the Graphics2D context. Any scaling, rotation, translation, or skewing, specified by the transform is also performed.
  2. The bounding box is then clipped according to the current clip region.
  3. The image pixels are composited onto the rendering surface according to the current composite attribute.

2.1.2 Controlling Rendering Quality

When graphics primitives are rendered on raster-graphics display devices, their edges can appear jagged due to aliasing. Arcs and diagonal lines take on a jagged or staircase appearance because they can only be displayed by approximating them using pixels. This is particularly noticeable on low-resolution devices. Figure 2-1 shows an example of aliased text drawn on a low-resolution device.

Figure 2-1 Aliased Text

Antialiasing is a technique used to render objects with smoother-appearing edges. This technique requires additional computing resources and can impact rendering speed. The Java 2D API lets you indicate whether you want objects to be rendered as quickly as possible, or whether you prefer that the rendering quality is as high as possible. Your preference is specified as a hint because not all platforms support modification of the rendering mode.

You can specify rendering preferences in the Graphics2D context by calling setRenderingHints. There are two types of hints:

The default values favor speed over quality--by default, antialising is not used and the performance hint is set to speed.


2.1.3 Stroking Shapes

When one of the Graphics2D draw methods is called, the outline of the specified Shape is rendered using the Stroke and Paint attributes in the Graphics2D context.

Stroking a Shape such as a GeneralPath object is equivalent to running a logical pen along the segments of the GeneralPath. The Stroke attribute defines the characteristics of the mark drawn by the pen, such as the width and dashing pattern. The Paint defines the color or pattern of the mark drawn by the pen.

For example, when draw(myRectangle) is called:

  1. The Stroke is applied to the rectangle's outline.
  2. The stroked outline is converted to a Shape object.
  3. The Paint is applied to the pixels that lie within the contour of the outline Shape.

Figure 2-2 Stroking a Shape

The Java 2D API provides a BasicStroke class that contains characteristics such as the line width, end-cap style, segment join-style, and the dashing pattern. The end-cap styles are:

The join styles are:

For example, the first image in Figure 2-3 uses the miter join-style; the second image uses a round join-style, a round end-cap style, and a dashing pattern.

Figure 2-3 Stroke Styles

To add or change the Stroke attribute in the Graphics2D context, you call setStroke.


2.1.4 Filling Shapes

When a Graphics2D fill method is called, the Paint style in the Graphics2D context is applied to all of the pixels that lie inside of the Shape's contour.

Using the Color class, you can specify a simple solid fill color for a shape. Color is the simplest implementation of the Paint interface. To fill shapes with more complex paint styles such as gradients and textures, you use the Java 2D paint classes GradientPaint and TexturePaint. These classes eliminate the time-consuming task of creating complex fills using simple solid-color paints.

Figure 2-4 Complex Fill Styles

When fill is called to render a Shape, the system:

  1. Determines what pixels comprise the shape
  2. Gets the color of each pixel from the Paint object
  3. Converts the color to an appropriate pixel value for the output device
  4. Writes the pixel to that device


    Batch Processing
    To streamline the processing of pixels, the Java 2D API processes them in batches. A batch can be either a contiguous set of pixels on a given scanline or a block of pixels. This batch processing is done in two steps:

    1. The Paint object's createContext method is called to create a PaintContext. The PaintContext stores the contextual information about the current rendering operation and the information necessary to generate the colors. The createContext method is passed the bounding boxes of the graphics object being filled in user space and in device space, the ColorModel in which the colors should be generated, and the transform used to map user space into device space. The ColorModel is treated as a hint because not all Paint objects can support an arbitrary ColorModel. (For more information about ColorModels, see "Color" on page 81.")
    2. The getColorModel method is called to get the ColorModel of the generated paint color from the PaintContext.
    3. The getRaster method is then called repeatedly to get the Raster that contains the actual color data for each batch. This information is then passed to the next stage in the rendering pipeline, which draws the generated color using the current Composite object.

You add a Paint to the Graphics2D context by calling setPaint. Simple solid color fills can also be specified with the setColor method.


2.1.5 Clipping

A clipping path identifies the portion of a Shape or Image that needs to be rendered. When a clipping path is part of the Graphics2D context, only those parts of a Shape or Image that lie within the path are rendered.

To add a clipping path to the Graphics2D context, you call setClip. Any Shape can be used to define the clipping path. To change the clipping path, you can either use setClip to specify a new path or call clip to change the clipping path to the intersection of the old clipping path and a new Shape.


2.1.6 Compositing

When two graphic objects overlap, it is necessary to determine what colors to render the overlapping pixels. For example, if a red rectangle and a blue rectangle overlap, the pixels that they share could be rendered red, blue, or some combination of the two. The color of the pixels in the overlapping area will determine which rectangle appears to be on top and how transparent it looks.

A color's alpha value is a measure of its transparency; it indicates, as a percentage, how much of a previously rendered color should show through when colors overlap. Opaque colors (alpha=1.0) don't allow any of the underlying color to show through, while transparent colors (alpha=0.0) let all of the underlying color show through.

The Composite object in the Graphics2D context defines how overlapping colors should be rendered.

The AlphaComposite class, an implementation of the Composite interface, supports a number of different compositing styles. Instances of this class embody a compositing rule that describes how to blend a new color with an existing one. One of the most commonly used compositing rules in the AlphaComposite class is SRC_OVER, which indicates that the new color (the source color) should be blended over the existing color (the destination color).


AlphaComposite Composition Rule

Description

Example

CLEAR

Clear

DEST_IN

Destination In

DEST_OUT

Destination Out

DEST_OVER

Destination Over

SRC

Source

SRC_IN

Source In

SRC_OUT

Source Out

SRC_OVER

Source Over


2.1.6.1 Managing Transparency

The alpha value used for rendering is derived from a Color, Paint, or Image object. When a Shape or text is antialiased, the Color or Paint alpha is combined with pixel coverage information from the rasterized path.

When you construct an AlphaComposite object, you can specify an additional alpha value. When you add this AlphaComposite object to the Graphics2D context, this extra alpha value increases the transparency of any graphic objects that are rendered--the alpha value of each graphic object is multiplied by the AlphaComposite's alpha value.


2.1.6.2 Transparency and Images

Images can carry transparency information for each pixel in the image. This information, called an alpha channel, is used in conjunction with the Composite object in the Graphics2D context to blend the image with existing drawings.

Figure 2-5 contains three images with different transparency information. In each case, the image is displayed over a blue rectangle. This example assumes that the Graphics2D context contains an AlphaComposite object that uses SRC_OVER as the compositing operation.

Figure 2-5 Transparency and Images

In the first image, all of the pixels are either fully opaque (the dog's body) or fully transparent (the background). This effect is often used on web pages. In the second image, all of the pixels in the dog's body are rendered using a uniform, non-opaque alpha value, allowing the blue background to show through. In the third image, the pixels around the dogs face are fully opaque (alpha=1.0), but as the distance from its face increases, the alpha values for the pixels decrease.



CONTENTS | PREV | NEXT
Copyright © 1997-1998 Sun Microsystems, Inc. All Rights Reserved.