Java Reference
In-Depth Information
How It Works
You set the color for drawing and filling the stars differently, simply to show that you can get the outline
as well as the fill. The stars are displayed in green with a blue boundary. It is important to draw the out-
line after the fill, otherwise the fill might encroach on the outline. You can fill a shape without drawing a
boundary for it — just call the
fill()
method. You could amend the example to do this by deleting the
last two statements in the inner loop. Now all you get is the green fill for each shape — no outline.
Gradient Fill
You are not limited to filling a shape with a uniform color. You can create a
java.awt.GradientPaint
ob-
ject that represents a graduation in shade from one color to another and pass that to the
setPaint()
method
for the graphics context. There are four
GradientPaint
class constructors:
•
GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2)
Defines a gradient from point
p1
with the color
c1
to the point
p2
with the color
c2
. The
color varies linearly from color
c1
at point
p1
to color
c2
at point
p2
•
GradientPaint(float x1, float y1, Color c1, float x2, float y2, Color c2)
By default the gradient is acyclic, which means the color variation applies only between the
two points. Beyond either end of the line the color is the same as the nearest end point.
•
GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2, boolean cyclic)
This does same as the previous constructor but with the points specified by their coordinates.
With
cyclic
specified as
false
, this is identical to the first constructor. If you specify
cyc-
lic
as
true
, the color gradation repeats cyclically off either end of the line — that is, you
get repetitions of the color gradient in both directions.