主动渲染和 EDT(Swing 动画)
我应该如何在 Swing 小程序中运行动画?
我有一个执行主动渲染的动画线程,它最初的动画效果很好。 有时(1 分钟到 2 小时后)它开始无法更新屏幕,并且只出现声音。 我认为这是因为绘制不是从 EDT 执行,导致某种并发问题。
那么,是否应该仅从 EDT 调用主动渲染(即 getGraphics() 和绘画)? 这样做的一个问题是 Swing 计时器缺乏精度。
或者是否有人在不使用 EDT 的情况下成功进行主动渲染,并完全禁用对页面的任何 EDT 更新(可能使用 Canvas / 或忽略 JPanel 上的重绘)?
How should I run animation in a Swing applet?
I have an animation thread performing active rendering and it initially animates fine. Sometimes (anywhere from 1 minute to 2 hours later) it begins to fail to update the screen and only the sounds occur. I believe this is due to the fact that the paint is not performed from the EDT causing some kind of concurrency problem.
So, should the active rendering (ie getGraphics() and painting) be called only from the EDT? A problem with this is the Swing timer lacks precision.
Or has anyone had success with active rendering without using the EDT, and completely disabling any EDT updates to the page (maybe using Canvas / or ignore repaint on a JPanel)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在另一个线程中将图形绘制到您自己的屏幕外图像中,并在 EDT 中复制到屏幕上。 但对于单线程的东西,我希望你的帧速率足够高,能够在 EDT 中完成。
You can paint graphics into your own off-screen image in another thread and copy to the screen in the EDT. But for single threaded stuff, I would hope your frame rate is high enough to be able to do it in the EDT.
需要注意的一些事情是确保除非必要,否则每次都只重新绘制需要重新绘制的内容,而不是整个图形上下文。 还有一个 计时框架,您可以使用它来处理一些动画。 我不认为它正在积极开发,但上次我查看它时,它有一些很好的 api 可用于动画。
在不知道您的具体用例的情况下,这就是我能想到的全部内容。
A few things to look at would be to make sure you are only repainting what needs to be repainted and not the whole graphics context each time unless necessary. Also there is a timing framework that you can use to handle some animations. I don't think it is actively being developed but last time I looked at it it had some nice api's to use for animation.
Without knowing your specific use case this is all I could come up w/ off the top of my head.