CONTENTS | PREV | NEXT | Java 2D API |
The GradientPaint class provides an easy way to fill a shape with a gradient of one color to another. When you create a GradientPaint, you specify a beginning position and color, and an ending position and color. The fill color changes proportionally from one color to the other along the line connecting the two positions. For example:
In the third star in Figure 2-6, both points lie within the shape. All of the points along the gradient line extending beyond P1 take the beginning color, and the points along the gradient line extending beyond P2 take the ending color.To fill a shape with a gradient of one color to another:
In the following example, a rectangle is filled with a blue-green gradient.
GradientPaint gp = new GradientPaint(100.0f, 100.0f,
200.0f, 200.0f, Color.green);
g2.setPaint(gp);
g2.fillRect(0, 0, w, h);