<< Chapter < Page Chapter >> Page >

If you compare the circles in the upper-left and upper-right quadrants in Figure 1 , you will see that they look considerably different.

Difference caused by parameter to the setPaint method

If you compare the code in Listing 6 with the code in Listing 7 , you will see that except for the coordinate values that position the circle, the onlydifference is the parameter that is passed to the setPaint method.

Listing 7 - Draw a circle with a gradient fill in the upper-right quadrant.
//Upper right quadrant //Gradient GREEN to BLUE, cyclic along horizontal// axis. Ellipse2D.Double circle2 =new Ellipse2D.Double(0.0,-128,128,128); g2.setPaint(new GradientPaint(64,0,Color.GREEN, 64,-32,Color.BLUE,true));g2.fill(circle2); g2.draw(circle2);

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 used 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 gradually 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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask