Java Reference
In-Depth Information
you like. You can remove all the segments in a path by calling the
reset()
method for the
GeneralPath
object. This empties the path.
The
GeneralPath
class implements the
Shape
interface, so a
Graphics2D
object knows how to draw a
path. You just pass a reference to a
GeneralPath
object as the argument to the
draw()
method for the graph-
ics context. To draw the path,
p
, that was defined in the preceding example in the graphics context
g2D
, you
would write the following:
g2D.draw(p); // Draw path p
Let's try an example.
TRY IT OUT: Reaching for the Stars
You won't usually want to construct a
GeneralPath
object as I did in the preceding example. You prob-
ably want to create a particular shape — a triangle or a star, say — and then draw it at various points
on a component. You might think you can do this by subclassing
GeneralPath
, but the
GeneralPath
class is declared as
final
so subclassing is not allowed. However, you can always add a
GeneralPath
object as a member of your class. You can try drawing some stars using your own
Star
class. You'll use
Here's the code for a class that can create a path for a star:
import java.awt.geom.*;
public class Star {
// Return a path for a star at x,y
public static GeneralPath starAt(float x, float y) {