【问题标题】:Is it possible to perform active rendering in Java Swing without being on the EDT?是否可以在不使用 EDT 的情况下在 Java Swing 中执行主动渲染?
【发布时间】:2009-12-27 18:54:04
【问题描述】:

我正在研究使用 Buffer Strategy 和 Javadoc 中描述的以下技术:

// Main loop
while (!done) {
 // Prepare for rendering the next frame
 // ...

 // Render single frame
 do {
     // The following loop ensures that the contents of the drawing buffer
     // are consistent in case the underlying surface was recreated
     do {
         // Get a new graphics context every time through the loop
         // to make sure the strategy is validated
         Graphics graphics = strategy.getDrawGraphics();

         // Render to graphics
         // ...

         // Dispose the graphics
         graphics.dispose();

         // Repeat the rendering if the drawing buffer contents 
         // were restored
     } while (strategy.contentsRestored());

     // Display the buffer
     strategy.show();

     // Repeat the rendering if the drawing buffer was lost
 } while (strategy.contentsLost());

}

在执行动画时最好避免使用EDTinvokeLaterinvokeAndWait

我的问题:

  • 如果这是在 Swing 应用程序中,我们不需要担心在 EDT 上调用 show 吗?
  • 其他人可以看到在 Swing 应用程序中使用它有什么问题吗?

这是受到interesting answer 游戏编程的启发。

【问题讨论】:

    标签: java swing animation event-dispatch-thread


    【解决方案1】:

    一般来说,不会。在event dispatch thread (EDT) 以外的线程上绘制会导致不希望的伪影,尽管结果可能是可以接受的。这个example 展示了一些权衡。我更喜欢使用javax.swing.Timer 安排在 EDT 上绘图,但other approaches 是可能的。此外,您选择的顶级Container 可能已经实现了Buffer Strategy

    【讨论】:

      【解决方案2】:

      正如垃圾狗所说,这样做不是一个好主意。创建自己的动画线程,将绘制离屏图像。然后将其推到某个支架上。如果调用paintComponent 获取当前图像并将其绘制在组件上。完毕。看会合模式。

      【讨论】:

        猜你喜欢
        • 2010-10-15
        • 2021-09-16
        • 2014-04-02
        • 1970-01-01
        • 2021-10-14
        • 2021-09-16
        • 2011-10-17
        • 1970-01-01
        • 2021-09-01
        相关资源
        最近更新 更多