Java Reference
In-Depth Information
GeneralPath(int rule) : Creates an object with the winding rule specified by the argument. You
can specify the argument as WIND_NON_ZERO or WIND_EVEN_ODD .
GeneralPath(int rule, int capacity) : Creates an object with the winding rule specified
by the first argument and the number of path segments specified by the second argument. In any
event, the capacity is increased when necessary.
GeneralPath(Shape shape) : Creates an object from the Shape object that is passed as the argu-
ment.
You can create a GeneralPath object with the following statement:
GeneralPath p = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
A GeneralPath object embodies the notion of a current point of type Point2D from which the next path
segment is drawn. You set the initial current point by passing a pair of ( x,y ) coordinates as values of type
float to the moveTo() method for the GeneralPath object. For example, for the object generated by the
previous statement, you could set the current point with the following statement:
p.moveTo(10.0f,10.0f); // Set the current point to 10,10
When you add a segment to a general path, the segment is added starting at the current point, and the
end of the segment becomes the new current point that is used as the starting point for the next segment. Of
course, if you want disconnected segments in a path, you can call moveTo() to move the current point to
wherever you want before you add a new segment. If you need to get the current position at any time, you
can call the getCurrentPoint() method that returns it as a reference of type Point2D .
You can use the following methods to add segments to a GeneralPath object:
void lineTo(float x, float y) : Draws a line from the current point to the point ( x , y ).
void quadTo(float ctrlx, float ctrly, float x2, float y2) : Draws a quadratic curve
segment from the current point to the point ( x2 , y2 ) with ( ctrlx , ctrly ) as the control point.
void curveTo(float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x2,
float y2) : Draws a Bezier curve segment from the current point with control point ( ctrlx1 ,
ctrly1 ) to ( x2 , y2 ) with ( ctrlx2 , ctrly2 ) as the control point.
Each of these methods updates the current point to be the end of the segment that is added. A path can
consist of several subpaths because a new subpath is started by a moveTo() call. The closePath() method
closes the current subpath by connecting the current point at the end of the last segment to the point defined
by the previous moveTo() call.
I can illustrate how this works with a simple example. You could create a triangle with the following
statements:
GeneralPath p = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
p.moveTo(50.0f, 50.0f); // Start point for path
p.lineTo(150.0f, 50.0f); // Line from 50,50 to 150,50
p.lineTo(150.0f, 250.0f); // Line from 150,50 to 150,250
p.closePath(); // Line from 150,250 back to start
The first line segment starts at the current position set by the moveTo() call. Each subsequent segment
begins at the end point of the previous segment. The closePath() call joins the latest end point to the point
set by the previous moveTo() call — which in this case is the beginning of the path. The process is much
the same using quadTo() or curveTo() method calls, and of course you can intermix them in any sequence
Search WWH ::




Custom Search