How do I create a Graphics object?
Created May 7, 2012
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) {
g.drawLine(10,10,20,20);
}
}
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();