Set the Graphics2D Context…

As mentioned briefly, the graphics context is a collection of state attributes specifying properties of the rendering. State attributes are sometimes also referred to as rendering attributes. Five interfaces and several classes that are part of java.awt package represent these attributes.

The first three interfaces are relevant to most development projects and are listed in Table 3.1. Note that these interfaces are implemented in the set of classes listed in Tables 3.2 and 3.3.

Table 3.1. Commonly Used Interfaces Implemented with the Graphics2D Context
Interface Description
Composite Methods to compose a primitive over the underlying Graphics area.
Paint Methods to specify rules for generating color patterns. Extends Transparency.
Stroke Methods to obtain a Shape representing the style of a line.

Table 3.2. Classes Implementing the Interfaces
Class Interface Description
AlphaComposite Composite Implements rules for composite overlays.
Color Paint Defines a solid color.
GradientPaint Paint Defines a gradient paint pattern.
TexturePaint Paint Defines a texture pattern.
BasicStroke Stroke Defines shapes to represent a pen style for a line drawing.

Two other interfaces are necessary for certain optimizations but are less commonly used by the programmer. The design of classes that use these interfaces are beyond the scope of this book.

Table 3.3. Less Frequently Used Interfaces Necessary for Optimized Context Operations
Interface Description
CompositeContext Defines an optimized and encapsulated environment for compositing.
PaintContext Defines an optimized and encapsulated environment for color pattern generation.

To make a visual effect, the programmer sets rendering attributes of the context according to the desired effect. In the BasicRecipeJ2D example from Listing 3.1, the visual effect we wanted was rather simple—we wanted the square painted red—so we changed the current color (one of many available state attributes) to red by changing the attribute with setColor(Color.Red). Note that the object we passed to the setColor() method is an instance of the Color class (which as shown in Table 3.2, implements the Paint interface).

The methods in Table 3.4 also have complementary methods for getting the current state attributes. The getBackground(), getComposite(), getPaint(), and getStroke() methods will return the corresponding current context attributes.

Table 3.4. Commonly Used Graphics2D Methods for Changing the Graphics Context
Method Description
setBackground() Sets the background color for the Component.
setComposite() Sets the compositing rule for subsequent rendering.
setPaint(Paint) Sets the current paint texture to use for rendering.
setStroke(Stroke) Sets the stroke style for rendering lines.

Another important set of attributes, called RenderingHints, allows the programmer to set state attributes for rendering options such antialiasing, dithering, and the interpolation method. The rendering methods will affect the tradeoff between the look of the output versus the speed of rendering. See the following URL for a complete list: http://java.sun.com/j2se/1.3/docs/api/java/awt/RenderingHints.html

Three other general types of rendering attributes can be set. The first is the font to use for rendering text. Some details of setting the font are covered later. The clipping path and transform, which are changed with the clip() and setTransform() methods, respectively, are covered later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset