File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Jumpy program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jumpy program" Watch "Jumpy program" New topic

Jumpy program

Ronan Jacksn

Joined: Jul 22, 2011
Posts: 4
I'm trying to create a game in a JFrame and JPanel however the app's animation/movement is 'jumping' every 1/4 of a second and is annoyingly visible.

Game Thread:


Is there something wrong with this update loop?

Darryl Burke

Joined: May 03, 2008
Posts: 5115

1. Never call setBackground(...) or otherwise change the state of any component in a painting method override.
2. Instead of a Thread with an infinite loop and a sleep(...) it's better you use a javax.swing.Timer. That's what the class is meant for.

luck, db
There are no new questions, but there may be new answers.
Ronan Jacksn

Joined: Jul 22, 2011
Posts: 4
Thanks. I've moved the setBackground out of the paint function. What is wrong with a thread with an infinite loop? Still jumping so I assume it is because of this thread?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
You also need to work out how long timeLeft is. You will see jerkiness of you update less than about 40 times a second.
Ronan Jacksn

Joined: Jul 22, 2011
Posts: 4
It's not constantly jerky. It goes smooth for about 3 seconds then jumps for a very small amount of time, then smooth for 3 seconds repeat etc...
I agree. Here's the link:
subject: Jumpy program
It's not a secret anymore!