Java Reference
In-Depth Information
A Graphics2D object maintains a whole heap of information that determines how things are drawn. Most of
this information is contained in six attributes within a Graphics2D object:
Paint: Determines the drawing color for lines. It also defines the color and pattern to be used
for filling shapes. The paint attribute is set by calling the setPaint(Paint paint) method
for the graphics context. java.awt.Paint is an interface that is implemented by the Color
class that defines a color. It is also implemented by the java.awt.GradientPaint and
java.awt.TexturePaint classes, which represent a color pattern and a texture, respectively. You
can therefore pass references of any of these types to the setPaint() method. The default paint
attribute is the color of the component.
Stroke: Defines a pen that determines the line style, such as solid, dashed, or dotted lines, and the
line thickness. It also determines the shape of the ends of lines. The stroke attribute is set by call-
ing the setStroke(Stroke s) method for a graphics context. The default stroke attribute defines
a square pen that draws a solid line with a thickness of one user coordinate unit. The ends of the
line are square, and joins are mitered (i.e., the line ends are beveled where lines join so they fit
together). The java.awt.Stroke interface is implemented by the java.awt.BasicStroke class,
which defines a basic set of attributes for rendering lines.
Font: Determines the font to be used when drawing text. The font attribute is set by calling the
setFont(Font font) method for the graphics context. The default font is the font set for the
component.
Transform: Defines the transformations to be applied during the rendering process. What you
draw can be translated, rotated, and scaled as determined by the transforms currently in effect.
There are several methods for applying transforms to what is drawn, as you see later. The default
transform is the identity transform, which leaves things unchanged.
Clip: Defines the boundary of an area on a component. Rendering operations are restricted so that
drawing takes place only within the area enclosed by the clip boundary. The clip attribute is set by
calling one of the two setClip() methods for a graphics context. With one version of setClip()
you define the boundary of the area to be rendered as a rectangle that you specify by the coordin-
ates of its top-left corner and the width and height of the rectangle. The other setClip() method
expects the argument to be a reference of type java.awt.Shape . The Shape interface is imple-
mented by a variety of classes in the java.awt.geom package that define geometric shapes of
various kinds, such as lines, circles, polygons, and curves. The default clip attribute is the whole
component area.
Composite: Determines how overlapping shapes are drawn on a component. You can alter the
transparency of the fill color of a shape so an underlying shape shows through. You set the com-
posite attribute by calling the setComposite(Composite comp) method for the graphics context.
The default composite attribute causes a new shape to be drawn over whatever is already there,
taking account of the transparency of any of the colors used.
All the objects that represent attributes are stored as references within a Graphics2D object. Therefore,
you must always call a setXXX() method to alter an attribute in a graphics context, not try to modify an
external object directly. If you externally alter an object that has been used to set an attribute, the results are
unpredictable.
Search WWH ::




Custom Search