JApplet - super.paint(); causes flicker

做~自己de王妃 提交于 2019-11-26 22:13:22

问题


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);
}

回答1:


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



来源:https://stackoverflow.com/questions/7004866/japplet-super-paint-causes-flicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!