【问题标题】:JApplet - super.paint(); causes flickerJApplet - super.paint();导致闪烁
【发布时间】:2011-10-23 17:00:24
【问题描述】:

我现在正在编写一个 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);
}

【问题讨论】:

  • 不要使用双缓冲,除非你真的必须这样做。默认情况下,JPanels 已经是双缓冲的。另外,是的,您应该使用paintComponents()。
  • @iAndrOidOs:paintComponents 是一种完全不同且不合适的使用方法。 paintComponent 是 OP 应该使用的。

标签: java swing paint flicker japplet


【解决方案1】:

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

编辑 1
您也不能为此使用paintComponents,因为它的作用完全不同。再次使用paintComponent,但只能在从JComponent 派生的组件中使用,例如JPanel(或JComponent 本身)。

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

【讨论】:

  • 不幸的是,如果我使用paintComponents,我的“屏幕”类将无法绘制。它本质上是一个具有 Init()、Update(Graphics) 和 Draw(Graphics) 函数的类。它还有两个变量,Image buffer 和 Graphics buffer_g,它们是屏幕的缓冲区变量。在 draw 函数中,它只是绘制缓冲区,当鼠标单击/拖动时,它会在缓冲区中添加线条。使用 paitnComponents 时,这不起作用。
  • @user830713:请重新阅读我的回答。你看到我关于不使用“paintComponents”的评论了吗?
  • 抱歉,我错过了“使用 JPanel”部分。它现在完美运行!谢谢!
  • paintComponents, @Override +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 2012-07-23
相关资源
最近更新 更多