How can I extend the Graphics class to add a new drawing method?

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);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact