Java Reference
In-Depth Information
This defines the upper-right quarter segment of the whole ellipse as an open arc. The angles are measured
counterclockwise from the horizontal in degrees. As shown in
Figure 19-10
,
the first angular argument is
where the arc starts, and the second is the angular extent of the arc.
Of course, a circle is just an ellipse for which the width and height are the same, so the following state-
ment defines a circle with a diameter of 150:
double diameter = 150.0;
Ellipse2D.Double circle = new Ellipse2D.Double(
position.x, position.y, // Top-left corner
diameter, diameter); // width & height of rectangle
This presumes the point
position
is defined somewhere. You often want to define a circle by its center
and radius — adjusting the arguments to the constructor a little does this easily:
Point2D.Double center = new Point2D.Double(200, 200);
double radius = 150;
Ellipse2D.Double newCircle = new Ellipse2D.Double(
center.x-radius, center.y-radius, // Top-left corner
2*radius, 2*radius); // width & height of rectangle
The fields that stores the coordinates of the top-left corner of the enclosing rectangle and the width and
height are public members of
Ellipse2D
and
Arc2D
objects. They are
x
,
y
,
width
, and
height
, respectively.
An
Arc2D
object also has public members,
start
and
extent
, that store the angles.
TRY IT OUT: Drawing Arcs and Ellipses
Let's modify the
paint()
method in
SketcherView.java
once again to draw some arcs and ellipses.
Replace the code in the body of the
paint()
method with the following:
@Override
public void paint(Graphics g) {
// Temporary code - to be deleted later...
Graphics2D g2D = (Graphics2D)g;
// Get a 2D
device context
Point2D.Double position = new Point2D.Double(50,10); // Initial
position
double width = 150;
// Width of
ellipse
double height = 100;
// Height of
ellipse
double start = 30;
// Start
angle for arc
double extent = 120;
// Extent of
arc
double diameter = 40;
// Diameter
of circle