How can I extend the Graphics class to add a new drawing method?
Created May 7, 2012
Scott Stanchfield
You're not supposed to extend it.
The AWT manager creates and passes Graphics objects to you to use to paint. It's the only thing that really knows how to set them up properly.
If you'd like to create utility functions to paint different things, you can create a new class that you pass the graphics context to.
Suppose you wanted to draw a Dragon (fractal) you could create
public class Dragon {
public static void draw(Graphics g) {
g.draw...
}
}
and from your paint method
public void paint(Graphics g) {
Dragon.draw(g);
}
}