To make things clear, I'm looking for the best possible alternative to implement a 'game loop' in Java (using Swing).
Currently, I'm using the following setup with paintComponent() overridden in the JPanel class for drawing stuff like so:
public void run() {
while (running) {
panel.update();
panel.repaint();
try {
sleep(SLEEP_TIME);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
This setup seems to work neatly, but moving shapes and images seems to stutter for a pixel or two at times. In other words, the movement isn't as smooth as I'd like it to be.
I'm wondering if there's a way to do the game loop more efficiently, i.e. to avoid stuttering in the movement?
SLEEP_TIME? Swing can normally achieve very high frame rates. The stuttering is likely due to some way your program has been written and the small sample you've provided is not enough to determine the problem. See stackoverflow.com/help/mcve