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

use*_*713 1 java swing paint flicker japplet

我现在正在编写一个 JApplet,每当我调用 super.paint() 时,小程序就会闪烁。我正在使用双缓冲(绘制图像,然后渲染该图像),但我认为 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);
}
Run Code Online (Sandbox Code Playgroud)

屏幕绘制方法

public void Draw(Graphics g)
{

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

Hov*_*els 5

不要使用绘图,也不要直接在 JApplet 中绘图。相反,在 JPanel 的 PaintComponent 方法中绘制并调用 super.paintComponent(g) 作为该方法的第一行。将该 JPanel 添加到 JApplet 的 contentPane 以允许小程序显示它。

编辑 1
另外,您不能为此使用 PaintComponent ,因为这会执行完全不同的操作再次使用paintComponent,但仅在从JComponent 派生的组件中使用,例如JPanel(或JComponent 本身)。

编辑2 另外,始终在您的paintComponent方法上方放置一个@Override,以确保您实际上覆盖了super方法。