Java Reference
In-Depth Information
Arcs and Ellipses
There are shape classes defining both arcs and ellipses. The abstract class representing a generic ellipse is:
Ellipse2D : This is the abstract base class for the Ellipse2D.Double and Ellipse2D.Float
classes that define ellipses. An ellipse is defined by the top-left corner, width, and height of the
rectangle that encloses it.
Arc2D : This is the abstract base class for the Arc2D.Double and Arc2D.Float classes that define
arcs as a portion of an ellipse. The full ellipse is defined by the position of the top-left corner and
the width and height of the rectangle that encloses it. The arc length is defined by a start angle
measured in degrees counterclockwise relative to the horizontal axis of the full ellipse, plus an
angular extent measured anticlockwise from the start angle in degrees. You can specify an arc as
OPEN , which means the ends are not connected; as CHORD , which means the ends are connected by
a straight line; or as PIE , which means the ends are connected by straight lines to the center of the
whole ellipse. These constants are defined as static members of the Arc2D class.
Arcs and ellipses are closely related because an arc is just a segment of an ellipse. Constructors for the
Ellipse2D.Float and Arc2d.Float classes are shown in Figure 19-10 . To define an ellipse you supply the
data necessary to define the enclosing rectangle — the coordinates of the top-left corner, the width, and the
height. To define an arc you supply the data to define the ellipse, plus additional data that defines the seg-
ment of the ellipse that you want. The seventh argument to the arc constructor determines the type, whether
OPEN , CHORD , or PIE .
FIGURE 19-10
You could define an ellipse with the following statements:
Point2D.Double position = new Point2D.Double(10,10);
double width = 200.0;
double height = 100;
Ellipse2D.Double ellipse = new Ellipse2D.Double(
position.x, position.y, // Top-left corner
width, height); // width & height of rectangle
You could define an arc that is a segment of the previous ellipse with this statement:
Arc2D.Double arc = new Arc2D.Double(
position.x, position.y, // Top-left corner
width, height, // width & height of rectangle
0.0, 90.0, // Start and extent angles
Arc2D.OPEN); // Arc is open
 
 
Search WWH ::




Custom Search