<< Chapter < Page | Chapter >> Page > |
Draw the solid green filled ellipse in the upper-left quadrant
In case you are unfamiliar with the term, an ellipse is the 2D shape shown in the upper-left quadrant of Figure 3 . A circle is an ellipse with the major and minor axes having the same lengths.
Figure 3 - A drawing of an ellipse.
There is no circle class
Java does not provide a Circle2D.Double class but it does provide an Ellipse2D.Double class, which you can use to draw circles.
(The Graphics class also provides a drawOval method that can be used to draw circles but those circles won't suffice for whatwe will be doing in this module.)
Four constructor parameters
The constructor for the Ellipse2D.Double class requires four parameters of type double . The first two parameters are the X and Y coordinates of the upper-left corner of an imaginary rectangle.
The next two parameters are the width and the height of the imaginary rectangle.
The sides of the imaginary rectangle are parallel to the X and Y axes, but can be rotated using affine transforms.
An ellipse inside an imaginary rectangle
The ellipse is constructed inside the imaginary rectangle such that it is symmetrical about its horizontal and vertical axes and it touches all four sidesof the rectangle. (The documentation refers to the rectangle as a framing rectangle.)
How do you construct a circle?
If the rectangle is actually a square, then the ellipse becomes a circle.
Construct an ellipse inside an imaginary square
The first statement in Listing 6 constructs an object of type Ellipse2D.Double inside a 128x128 square that just fits in the upper left quadrant of our Cartesian coordinate system shown in Figure 1 . The circle object's reference is saved in the reference variable named circle1 .
Listing 6 - Draw the solid green filled ellipse in the upper-left quadrant. |
---|
//Upper left quadrant
Ellipse2D.Double circle1 =new Ellipse2D.Double(-128,-128,128,128);
//Solid GREEN fillg2.setPaint(Color.GREEN);
g2.fill(circle1);g2.draw(circle1); |
Set the painting color
The statement near the middle of Listing 6 sets the painting color to Color.GREEN .
(Note that Listing 6 calls setPaint whereas Listing 5 calls setColor . I will leave it as an exercise for the student to study the documentation in order to understand thedifference between setColor and setPaint .)
Fill the circle referred to by circle1
The second statement from the bottom in Listing 6 calls the fill method of the Graphics2D class passing the circle object's reference as a parameter. Although this can get quite complex, in this simplecase, it causes the circle object to be filled with the paint color (green) .
Draw the filled circle object
Finally, the last statement in Listing 6 calls the draw method of the Graphics2D class to cause the filled circle to be drawn inside the framing rectangle that was specified when the circle wasconstructed. This results in the filled green circle in the upper-left quadrant in Figure 1 .
Draw a circle with a gradient fill in the upper-right quadrant
This is where things tend to get a little complicated.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?