Book Home Java Enterprise in a Nutshell Search this book

4.2. Java 2D Graphics Attributes and Operations

Java 2D dramatically expands the graphics capabilities of Java. It does this through the java.awt.Graphics2D subclass of java.awt.Graphics. In Java 2, you can simply cast any Graphics object you are given to a Graphics2D object, and then you can use the new features of Java 2D.

Table 4-5 and Table 4-6 summarize the new features of Java 2D by listing the graphics attributes and graphics operations supported by the Graphics2D class.

Table 4-5. Graphics Attributes of Java 2D

Attribute Type Description
Foreground color Color

Inherited from Graphics but superseded by the fill style attribute and the Paint interface.

Background color Color

Inherited from Graphics but can now be set and queried with setBack- ground() and getBackground(). This attribute is still used only by clearRect().

Font Font

Inherited from Graphics. All of the system fonts are now available to Java.

Clipping region Shape

Inherited from Graphics. In Java 2D, however, arbitrary Shape objects may be used; the clipping region is no longer restricted to only rectangular shapes. A new method, which is called clip(), sets the clipping region to the intersection of the current region and a specified Shape.

Line style Stroke

A Stroke object specifies how lines are drawn. The BasicStroke implementation supports line width, dash pattern, and other attributes, described in more detail later in the chapter. Set the current line style with setStroke().

Fill style Paint

A Paint object specifies how an area is filled. Color implements this interface and fills with a solid color, java.awt.TexturePaint fills with a tiled image, and java.awt.Gra- dientPaint fills with a color gradient. Set the current fill style with setPaint().

Compositing Composite

A Composite object controls how the color of a pixel is combined, or composited, with the color of the pixel on top of which it is drawn. The default compositing operation combines translucent pixels with the pixels they overlay, letting the overlaid colors "show through." The AlphaComposite class is an implementation of Composite; it performs various types of compositing, based on the alpha-transparency of the pixels involved.

Transform

java.awt.geom.- AffineTransform

Controls the translation, scaling, rotation, and shearing of the coordinate system. Set this attribute with setTransform(), or modify the current transform with translate(), scale(), rotate(), shear(), or transform().

Hints RenderingHints

A RenderingHints object allows a program to express preferences about various speed versus quality trade-offs made by Java 2D. Most notably, RenderingHints controls whether Java 2D performs antialiasing. Set with setRenderingHints(), setRendering- Hint(), or addRenderingHints().

Table 4-6. Graphics Operations of Java 2D

Operation Methods Description
Drawing

draw(), inherited methods

draw() outlines an arbitrary Shape. Uses the clip, transform, stroke, paint, and composite attributes.

Filling

fill(), inherited methods

fill() fills an arbitrary Shape. Uses the clip, transform, paint, and composite attributes.

Hit detection

hit()

Tests whether a given rectangle (in device coordinates) intersects the interior or outline of an arbitrary Shape. Uses the clip, transform, and stroke attributes when testing the outline of a Shape.

Text drawing

drawString(), drawGlyphVector(), inherited methods

Java 2D defines text-drawing methods that take String, java.text.AttributedCharacter- Iterator, and java.awt.font.- GlyphVector arguments. Text drawing uses the clip, transform, font, paint, and composite attributes. Note, however, that AttributedCharacterIterator objects supply their own fonts.

Image drawing

drawImage(), drawRenderableImage(), drawRenderedImage(), inherited methods

Java 2D defines new image-drawing methods that draw special types of images. java.awt.image.BufferedImage is the most important new type. These methods use the clip, transform, and composite attributes.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.