When you set an attribute, you pass in the appropriate
attribute object. For example, to change the paint attribute to a blue-green
gradient fill, you would construct a GradientPaint
object and then
call setPaint
.
gp = new GradientPaint(0f,0f,blue,0f,30f,green); g2.setPaint(gp);
Graphics2D
holds references
to its 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 rendering causes unpredictable behavior.
Graphics2D
provides the following general
rendering methods that can be used to draw any geometry primitive, text, or
image:
draw
fill
--renders any geometry primitive by filling
its interior with the color or pattern specified by the paint attribute.
drawString
--renders any text string. The font attribute
is used to convert the string to glyphs, which are then filled with the color
or pattern specified by the paint attribute. drawImage
--renders the specified image. In addition, Graphics2D
supports the Graphics
rendering
methods for particular shapes, such as drawOval
and fillRect
.