简体   繁体   中英

JApplet - super.paint(); causes flicker

I'm writing a JApplet right now, and whenever I call super.paint(), the applet flickers. I am using double buffering (drawing to an image, and then rendering that image), but I think super.paint() is clearing the screen or something, defeating my double buffer.

I know I'm supposed to use paintComponents(), but for some reason, when I call "currentScreen.Draw(g)," it won't show the screen's draw.

Can anyone help me with this?

public void paint(Graphics g)
{   

    super.paint(g);//Remove this and it works, but the JApplet background color will be gone, and everything will be white.

    currentScreen.Draw(g);
}

Screen Draw Method

public void Draw(Graphics g)
{

    if(buffer != null)
        g.drawImage(buffer, 150, 0, null);
    //g.drawString(drawstring, x, y);
}

Don't use paint and don't draw directly in the JApplet. Instead draw in a JPanel's paintComponent method and call super.paintComponent(g) as the first line of that method. Add that JPanel to your JApplet's contentPane to allow the applet to display it.

Edit 1
Also you can't use paintComponent for this as this does something entirely different.因为它的作用完全不同。 Again use paintComponent but only in a component that derives from JComponent such as a JPanel (or a JComponent itself).

Edit 2 Also always put an @Override above your paintComponent method to be sure that you are in fact overriding the super method.

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