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

Problems animating and Problems closing JFrame

Neyo Anderson

Joined: Dec 04, 2012
Posts: 1
Can anyone give me any help? I am trying to use a Swing timer for the animation because using the run() method of the Thread class caused tons of flickering.

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 119
I would structure things a little differently.

I would do the "listening" on the JPanel, not the code launched by the Timer.

I would restrict code executed by the Timer to a single command: to call an update() method on the JPanel. But as I look closer, it appears that the Timer really isn't contributing anything to the animation. The way you have things set up, animation is the result of key inputs (each calling a repaint).

More normal use of the Timer is to use util.Timer, calling anywhere from 20 to 60 times per second. In this scenario, the velocity of the objects are stored in variables and used to update the x,y location of objects every animation cycle. The key input (listeners) would be used to alter the velocity values, and these would occur outside of the update/repaint cycle.

Hmm. A lot to explain--maybe check out some game-loop tutorials?
I agree. Here's the link:
subject: Problems animating and Problems closing JFrame
It's not a secret anymore!