Compare constructor parameters with upper-right quadrant
Now consider the parameter values used in Listing 7 and compare them with the image in the upper-right quadrant in
A simple color paint versus a gradient paint
Listing 6 passes an object of the simple Color class representing the color green to the setPaint method.
Listing 7 passes an object of the GradientPaint class to the setPaint method.
Therefore, we need to understand the behavior of an object of the GradientPaint class.
Constructor parameters
There are four overloaded constructors for the GradientPaint class. The constructor highlighted in yellow in Listing 7 is one of the most complicated. It requires the following parameters:
- x1 - x coordinate of the first specified Point in user space
- y1 - y coordinate of the first specified Point in user space
- color1 - Color at the first specified Point
- x2 - x coordinate of the second specified Point in user space
- y2 - y coordinate of the second specified Point in user space
- color2 - Color at the second specified Point
- cyclic - true if the gradient pattern should cycle repeatedly between the two colors; false otherwise
Two points and two colors
Basically, the class allows you to specify two points and two colors (plus one additional boolean parameter) when you construct an object of the class.
One of the colors is associated with each point.
An imaginary line segment
Think of the two points as being at the ends of an imaginary line segment, which can be at any angle relative to the horizontal.
Colors at the ends of the line segment
When a shape is drawn using a gradient fill, one of the colors will appear at one end of the line segment and the other color will appear at the other end ofthe line segment.
The color will change
The color will change from one color to the other along the imaginary line segment connecting the two points.
Perpendicular color bands on the sides
The colors extend out to the sides of the imaginary line segment in bands that are perpendicular to the line segment.
The cyclic parameter
If the last (cyclic) parameter is set to false, the color will only change along the imaginary line segment. Areas beyond each end of the linesegment will be the colors that are specified for the points at the ends of the line segment.
(This parameter was not set to false for any of the circles in Figure 1 .)
If the cyclic parameter is true...
The pattern of color change that occurs along the line segment will extend in a cyclic fashion beyond the ends of the line segment all the way to infinity.
Compare constructor parameters with upper-right quadrant
Now consider the parameter values used in Listing 7 and compare them with the image in the upper-right quadrant in Figure 1 .
The location of the first point
The diameter of each circle in Figure 1 is 128 pixels. The first point for the upper-right quadrant in Figure 1 is on the X axis at the point where the circle touches the X axis. This point is specified to have a color of green.
The location of the second point
The second point is 32 pixels directly above the first point. Therefore, the imaginary line segment is perpendicular to the X axis. It extends from the Xaxis one-fourth of the way to the top of the circle. The second point is specified to have a color of blue.