Swing 中的流畅动画
如何以客户端监视器的最大刷新率制作 JPanel 动画?
我希望发生的是 paintComponent
仅在上次更新后调用。我可以通过使用 Timer
每隔 (1000 / 60) 毫秒安排一次 repaint()
来完成此操作,但我只是猜测刷新率是多少。
实际上,我希望连续调用 repaint
。但是,如果我每秒调用 repaint
200 次,并且 EDT 不忙,它就会每秒执行 paintComponent
200 次,这对处理器时间的利用效率很低,可能会产生不利影响应用程序的其余性能。
How do I animate a JPanel at the maximum refresh rate of the client's monitor?
What I would like to happen is that paintComponent
is only called after the last update. I could do this by using a Timer
to schedule repaint()
every (1000 / 60) ms, but I'm just guessing at what the refresh rate is.
Effectively I'd like repaint
to be called continuously. But if I call repaint
200 times per second and the EDT isn't busy, it executes paintComponent
200 times per second, which is inefficient use of processor time and could aversely impact the rest of the application's performance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定以显示器的刷新率重新绘制是个好主意。但是,如果您想知道刷新率是多少,您可以使用这段代码(无耻地从互联网上复制)。
I am not sure repainting at the monitor's refresh rate is a good idea. But if you are looking to find out what the refresh rate is you can use this piece of code (shamelessly copied from the interwebs).
我同意
@little bunny foo foo
并且您也必须使用操作系统延迟进行计算,该值取决于硬件和硬件。 SW,那么我的WinXp的值约为63ms,通过超频此频率,您会从RepaintManager得到错误,例如JTable是非常敏感的JComponent,请阅读有关Swing中绘画的更多信息 paintImmediately(), repaint() 通过默认编辑创建 EDT:消除对 repaint() 的所有可能的疑虑;看这里绘画和javax.swing.Timer
I'm agreed with
@little bunny foo foo
and you have to calculate with OS Latency too, this value depends of HW & SW, then my WinXp has value around 63ms, by overlocking this frequency you get Error from RepaintManager, for example JTable is very sensitive JComponents, please read more about painting in Swing paintImmediately(), repaint() create EDT by defalutEDIT: to blow away all possible doubts about repaint(); look here painting and javax.swing.Timer