tags:

views:

93

answers:

2
+3  A: 

Quick Fix:

Change ImageAnimator() like this:

        lblDisplay.setIcon(images);
        lblDisplay.paintImmediately(getBounds());
        th.sleep(25);

The problem you're having is that setting an icon does not automatically cause Swing to repaint the component onscreen. Calling paintImmediately will do that.

General advice:

Animation in swing is normally done using the Swing Timer. You'll notice at the moment that your UI is unresponsive - once you've started the animation, you won't be able to stop it until its over. That's because all Swing events happen on a single thread - the Event Dispatch Thread. Running a loop with Thread.sleep(...) in the middle ties this thread up, leaving it unavailable to process other input (such as pressing the stop button)

This article helped me immeasurably when I was trying to understand how Swing handles concurrency, and The Swing Trail has lots of advice on using Swing effectively, painting custom components etc.

I'd also plug the demo code on Filthy Rich Clients, I'm working through the book at the moment and it's worth a look.

MHarris
and method names should start with a lower case letter so imageAnimator()... :)
willcodejavaforfood
Good call. I work with a couple of ex C++ gurus, and have gotten so used to their random approach to capitalisation, it didn't even register. :(
MHarris
Thanks for the advice, but the paintImmediately() method did not work as it keeps telling me that it can't find symbol. But I'll be switching to the swing timer.
HH
That'll teach me to double check things I've not used in a while.I think lblDisplay.paintImmediately(lblDisplay.getBounds()) should do the trick, but you're better off on the swing timer anyway. :)
MHarris
Well,lblDisplay.paintImmediately(lblDisplay.getBounds()) did not do the trick, but rather lblDisplay.paint(lblDisplay.getGraphics()) did it. But i need to let the app run once before i can see the animation.
HH
A: 

This Java 2D games tutorial covers several basic animation techniques.

trashgod