简体   繁体   English

Java swing中的图形滞后

[英]Lagging graphics in Java swing

I have a program that bounces an arbitrary number of balls around a predefined window. 我有一个程序可以在预定义的窗口周围弹起任意数量的球。 It relies on a Swing Timer to update the balls according to a delay set by the user. 它依赖于摇摆计时器根据用户设置的延迟更新球。 My problem is this: the balls lag much more than they should under modest circumstances. 我的问题是:在适度的情况下,球滞后的程度远远超过应有的程度。 The weird thing is that the balls move smoothly if there is another action being performed (eg mouse click or mouse moving around the screen). 奇怪的是,如果执行其他操作(例如,鼠标单击或鼠标在屏幕上移动),则球会平滑移动。 Does anyone know what would cause this? 有人知道会导致什么吗?

The weird thing is that the balls move smoothly if there is another action being performed (eg mouse click or mouse moving around the screen). 奇怪的是,如果执行其他操作(例如,鼠标单击或鼠标在屏幕上移动),则球会平滑移动。

Based on that statement, I would guess that your problem is not properly calling repaint() on JPanel or other java.awt.Component subclass which is displaying the balls. 根据该声明,我想您的问题是未正确调用JPanel或其他显示球的java.awt.Component子类上的repaint() You need to call Component.repaint() whenever your code changes the position of the balls. 每当代码更改球的位置时,都需要调用Component.repaint()

Not sure if this might help: have you considered double buffering? 不知道这是否有帮助:您是否考虑过双重缓冲? (that is doing all expensive paint operations in an 'off-image' and copying that image into the visible area when done). (即在“离像”中进行所有昂贵的绘制操作,并在完成后将该图像复制到可见区域中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM