简体   繁体   中英

i have a problem in my paint class

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...

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:

  1. How to Make Applets - (you should be extending JApplet, not Applet)

  2. 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!

I think you want paint instead of Painter

public void paint(Graphics g2) {
    g2.setColor(Color.black);
    g2.drawRect(50, 50, 400, 400);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM