Reputation: 27
this is my code...
public class Paintexample extends Applet{
private Graphics g;
JPanel panel;
public void init()
{
this.setLayout(new BorderLayout());
this.panel=new JPanel();
this.panel.setPreferredSize(new Dimension(1024,500));
this.add(panel);
g=this.panel.getGraphics();
}
public void Painter(Graphics g2)
{
g2=this.panel.getGraphics();
g2.setColor(Color.black);
g2.drawRect(50, 50, 400, 400);
}
}
the computer write this :
Exception in thread "main" java.lang.NullPointerException at Paintexample.Painter(Paintexample.java:27) at Paintexample.init(Paintexample.java:22) at Main.main(Main.java:15)
I don't know way the applet go's down.... the graphics does not works..... he said the graphics are not init...
Upvotes: 0
Views: 147
Reputation: 324108
In one of your previous questions you were given a link to the Swing tutorial. I suggest you actually read the tutorial. It has sections on:
How to Make Applets - (you should be extending JApplet, not Applet)
Custom Painting - (this is done by overriding the paintComponent() method of a JPanel (or JComponent), NOT by overriding paint() of the JApplet class
For further help you need to improve your "accept rate". 0% is too low!
Upvotes: 3
Reputation: 108947
I think you want paint instead of Painter
public void paint(Graphics g2) {
g2.setColor(Color.black);
g2.drawRect(50, 50, 400, 400);
}
Upvotes: 2