How do I create a Graphics object?

Scott Stanchfield

The initial answer is "you don't; AWT does".

Graphics objects are your "window" into an image, whether that image is onscreen or offscreen.

If you're looking to paint onscreen, the only way you should be using a Graphics object is inside a paint() method. AWT will pass you a Graphics object, which you can use to do your painting onscreen.

public class Foo extends Canvas {
  public void paint(Graphics g) {

If you're looking to paint offscreen, you can ask the image you want to paint on for its Graphics object.

Image i = f.createImage(100, 100);	
Graphics g = i.getGraphics();