Java Reference
In-Depth Information
Angles may be negative. A negative starting angle sweeps clockwise from the easterly direc-
tion, as shown in Figure 13.12. A negative spanning angle sweeps clockwise from the starting
angle. The following two statements draw the same arc:
negative degrees
g.fillArc(x, y,
2
* radius,
2
* radius,
-30
,
-20
);
g.fillArc(x, y,
2
* radius,
2
* radius,
-50
,
20
);
The first statement uses negative starting angle
-30
and negative spanning angle
-20
, as
shown in Figure 13.12a. The second statement uses negative starting angle
-50
and positive
spanning angle
20
, as shown in Figure 13.12b.
-30
-50
-20
20
(a) Negative starting angle -30
and
and positive spanning angle 20
(b) Negative starting angle -50
negative spanning angle -20
F
IGURE
13.12
Angles may be negative.
13.14
Describe the methods for drawing/filling arcs.
13.15
Draw the upper half of a circle with radius
50
.
13.16
Fill the lower half of a circle with radius
50
using the red color.
✓
✓
Check
Point
You can draw a polygon or a polyline that connects a set of points.
Key
Point
To draw a polygon, first create a
Polygon
object using the
Polygon
class, as shown in
Figure 13.13.
java.awt.Polygon
+xpoints: int[]
+ypoints: int[]
+npoints: int
x
-coordinates of all points in the polygon.
y
-coordinates of all points in the polygon.
The number of points in the polygon.
+Polygon()
+Polygon(xpoints: int[], ypoints: int[],
npoints: int)
+addPoint(x: int, y: int): void
+contains(x: int, y: int): boolean
Creates an empty polygon.
Creates a polygon with the specified points.
Appends a point to the polygon.
Returns true if the specified point (x, y) is
contained in the polygon.
F
IGURE
13.13
The
Polygon
class models a polygon.
A polygon is a closed two-dimensional region. This region is bounded by an arbitrary
number of line segments, each being one side (or edge) of the polygon. A polygon comprises
a list of
(x, y)
-coordinate pairs in which each pair defines a vertex of the polygon, and two
successive pairs are the endpoints of a line that is a side of the polygon. The first and final
points are joined by a line segment that closes the polygon.