Java Reference
In-Depth Information
Gradient Fill
You are not limited to filling a shape with a uniform color. You can create a
GradientPaint
object
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:
Constructor
Description
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
.
By default the gradient is
acyclic
, which means
the color variation only applies between the two
points. Beyond either end of the line the color is
the same as the nearest end point.
GradientPaint
(float x1, float y1, Color c1,
float x2, float y2, Color c2)
The same as the previous constructor but with
the points specified by their coordinates.
GradientPaint
(Point2D p1, Color c1,
Point2D p2, Color c2,
boolean cyclic)
With the last argument specified as
false
, this
is identical to the first constructor. If you
specify cyclic 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.
GradientPaint
(float x1, float y1,
Color c1, float x2, float y2,
Color c2, boolean cyclic)
This is the same as the previous constructor
except for the explicit point coordinates.
Points off the line defining the color gradient
will have the same color as the normal (that is,
right-angle) projection of the point onto the
line.
This stuff is easier to demonstrate than to
describe, so here's the output from the
example we're about to code: