This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.26 Java3026: gradientpaint and other java2d classes  (Page 7/8)

The color change

As a result, the color changes from green to blue along the 32-pixel line segment. Color bands extend to the right and left, perpendicular to the linesegment.

The cyclic parameter

The cyclic parameter is set to true, so the pattern repeats along the remaining vertical dimension of the circle. The color changes from blue back togreen along the next 32-pixel vertical distance causing the circle to be green at the center.

The pattern repeats again resulting in a blue band three-fourths of the way from the bottom to the top of the circle and a green band at the top of thecircle.

That's all there is to it

Now that you know the scheme, you should be able to examine the code in Listing 8 for the remaining two quadrants and understand how the constructor parameters resulted in the color patterns thatyou see in Figure 1 .

Listing 8 - Code for the remaining two quadrants.
//Lower left quadrant //Gradient GREEN to BLUE, cyclic along vertical axis.//horizontal axis Ellipse2D.Double circle3 =new Ellipse2D.Double(-128,0.0,128,128); g2.setPaint(new GradientPaint( -128,-64,Color.GREEN,-107,-64,Color.BLUE,true)); g2.fill(circle3);g2.draw(circle3); //Lower right quadrant//Gradient GREEN to BLUE, cyclic along // 45 degree angle Ellipse2D.Double circle4 =new Ellipse2D.Double(0,0,128,128); g2.setPaint(new GradientPaint( 19,19,Color.GREEN,64,64,Color.BLUE,true)); g2.fill(circle4);g2.draw(circle4); }//end process}//end class Prob03Runner

An interesting anomaly

I will point out one interesting anomaly, however. Even though we shifted the origin to the center, we did not change the direction that represents positivevalues on the Y axis.

Coordinates above the center are negative and coordinates below the center are positive.

Where is the line segment?

If you examine the constructor parameters for the lower-left quadrant in Listing 8 , you will see that the imaginary line segment isn't in the lower-left quadrant.

The line segment is 21 pixels in length, parallel to the horizontal axis. However, it is 64 pixels above the horizontal axis. That is not in thelower-left quadrant.

The effect is...

The effect is as though the color gradient fills the universe, and is based on a line segment placed anywhere in the universe.

However, we only see the color gradient through the shape that we specify as a parameter to the fill method. In other words, that shape is a window through which we can see a background consisting of gradient colorchanges.

Our window is in the lower-left quadrant

In this case, that shape is the circle in the lower-left quadrant, so we see the gradient color effect in the lower-left quadrant.

How about the bottom-right quadrant

I stated earlier that the imaginary line segment can be at any angle relative to the horizontal axis.

For the bottom right quadrant, the line segment is 64 pixels in length. It lies along a line that goes through the origin and is at 45 degrees clockwiserelative to the horizontal.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.