can't getGraphics from JPanel

Hi,

I want to get the graphics context from a JPanel so I can draw on it. In the example below,theStage ends up null, causing a null pointer exception - where am I going wrong?

class winextends JFrame{

Container c;

win(){

stage stage =new stage();

c = getContentPane();

c.add(stage);

setSize(400,400);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

class stageextends JPanel{

stage(){

Graphics2D theStage = (Graphics2D)getGraphics();

theStage.drawLine(0,0,100,100);

}

}

}

big thanks for any help,

Des

[1127 byte] By [Desmond22a] at [2007-11-26 19:52:45]
# 1
Swing questions should go in the Swing forum.And the "drawLine" call should go in the paintComponent method. You need to override the JPanel's paintComponent method to do your drawing.The Graphics is null because the JPanel isn't visible yet.
doremifasollatidoa at 2007-7-9 22:43:53 > top of Java-index,Java Essentials,Java Programming...
# 2
An excellent rule of thum is to *never* use the Component method getGraphics().As mentioned, you should be overriding paintComponent instead.
DrLaszloJamfa at 2007-7-9 22:43:53 > top of Java-index,Java Essentials,Java Programming...