Java Reference
In-Depth Information
The
drawString()
method draws the string specified as the first argument at the position determined
by the second and third argument - these are the
x
and
y
coordinates in user coordinates of the bottom-
left corner of the first letter of the string. The string will be drawn by obtaining the glyphs for the
current
Font
object in the device context corresponding to the characters in the string. As we said
when we discussed
Font
objects, the glyphs for a font define the physical appearance of the characters.
However, there's more to drawing than is apparent from this example. The graphics context has
information about the line style to be drawn, as well as the color, the font to be used for text, and more
besides. Let's dig a little deeper into what is going on.
The Drawing Process
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:
The
paint
attribute 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.
Paint
is an interface that is implemented by the
Color
class that defines a color. It is also implemented by the
GradientPaint
and
TexturePaint
classes that are 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.
❑
The
stroke
attribute 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 calling the
setStroke(Stroke
s)
m
ethod for a graphics context.
The default stroke attribute defines a square pen that draws a solid line with a thickness of 1
user coordinate unit. The ends of the line are square and joins are mitered.
❑
The
font
attribute 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.
❑
The
transform
attribute 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 we
will see. The default transform is the identity transform, which leaves things unchanged.
❑
The
clip
attribute defines the boundary of an area on a component. Rendering operations are
restricted so that drawing only takes place 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. The
default clip attribute is the whole component area.
❑
The
composite
attribute 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 composite 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.
❑