0

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 (e.g. mouse click or mouse moving around the screen). Does anyone know what would cause this?

nickhirakawa
  • 247
  • 1
  • 12
  • 5
    *"Does anyone know what would cause this?"* The code. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 24 '12 at 22:20
  • 4
    Your code is blocking the event dispatch thread; the system generated `repaint()` events are not. See the working examples [here](http://stackoverflow.com/q/9849950/230513). – trashgod Mar 24 '12 at 22:25

2 Answers2

0

The weird thing is that the balls move smoothly if there is another action being performed (e.g. 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. You need to call Component.repaint() whenever your code changes the position of the balls.

ulmangt
  • 5,343
  • 3
  • 23
  • 36
0

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).

nansen
  • 2,912
  • 1
  • 20
  • 33