JApplet - super.paint();导致闪烁

时间:2011-08-10 01:12:27

标签: java swing paint flicker japplet

我现在正在编写JApplet,每当我调用super.paint()时,applet都会闪烁。 我正在使用双缓冲(绘制到图像,然后渲染该图像),但我认为super.paint()正在清除屏幕或其他东西,击败我的双缓冲区。

我知道我应该使用paintComponents(),但出于某种原因,当我调用“currentScreen.Draw(g)”时,它不会显示屏幕的绘制。

任何人都可以帮我吗?

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

屏幕绘制方法

public void Draw(Graphics g)
{

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

1 个答案:

答案 0 :(得分:4)

不要使用绘画,也不要直接在JApplet中绘制。而是在JPanel的paintComponent方法中绘制并调用super.paintComponent(g)作为该方法的第一行。将JPanel添加到JApplet的contentPane中以允许applet显示它。

编辑1
此外,您不能使用paintComponent s ,因为这会完全不同。再次使用paintComponent,但仅限于从JComponent派生的组件,例如JPanel(或JComponent本身)。

编辑2 还总是将@Override放在paintComponent方法之上,以确保实际上覆盖了super方法。