Java Reference
In-Depth Information
This creates an arc for an oval at 27, 22 that is 42 pixels wide by 30 pixels tall. The arc
begins at 33 degrees, extends 90 degrees clockwise, and is closed like a pie slice.
Polygons
Polygons are created in Java2D by defining each movement from one point on the poly-
gon to another. A polygon can be formed from straight lines, quadratic curves, or Bézier
curves.
The movements to create a polygon are defined as a GeneralPath object, which also is
part of the java.awt.geom package.
A GeneralPath object can be created without any arguments, as shown here:
GeneralPath polly = new GeneralPath();
The moveTo() method of GeneralPath is used to create the first point on the polygon.
The following statement would be used if you wanted to start polly at the coordinate
5, 0:
polly.moveTo(5f, 0f);
After creating the first point, the lineTo() method is used to create lines that end at a
new point. This method takes two arguments: the x,y coordinate of the new point.
The following statements add three lines to the polly object:
polly.lineTo(205f, 0f);
polly.lineTo(205f, 90f);
polly.lineTo(5f, 90f);
The lineTo() and moveTo() methods require float arguments to specify coordinate
points.
If you want to close a polygon, the closePath() method is used without any arguments,
as shown here:
polly.closePath();
This method closes a polygon by connecting the current point with the point specified by
the most recent moveTo() method. You can close a polygon without this method by using
a lineTo() method that connects to the original point.
After you have created an open or closed polygon, you can draw it like any other shape
using the draw() and fill() methods. The polly object is a rectangle with points at 5,
0; 205, 0; 205, 90; and 5, 90.
Search WWH ::




Custom Search