-1

Hi guys we're trying to make a simply animation in a JFrame. We have 13 images of a deck (from less to more cards) and we want to implement it in our code with a sleep function, something like this

setIcon(pic1)
sleep
setIcon(pic2)
sleep
etc...

So it will looks like that the deck is growing, we had used the function thread.sleep but it doesn't work. Any help?

Thanks.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • *"Any help?"* Any sign of effort? 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Feb 19 '14 at 22:40

1 Answers1

2

Search this site on this question and you'll see why Thread.sleep(...) is not good when called on the Swing event thread. You'll also see that you should be using a Swing Timer instead.

For example, please have a look at my code in my answer here.

In your situation, assuming an array of ImageIcons, you could try something like:

new Timer(timerDelay, new ActionListener() {
  private int iconIndex = 0;

  public void actionPerformed(ActionEvent evt) {
    if (iconIndex < iconArray.length) {
      label.setIcon(iconArray[iconIndex]);
      iconIndex++;
    } else {
      ((Timer)evt.getSource()).stop();  // stop the timer
    }
  }
}).start();

For the gory details, please check out the Swing Timer Tutorial.


Edit
You state in comment:

I don't have an array of ImageIcons, I have 13 images and a button, I want to make a external function (Out of the Button event) that makes appear 13 images in it every one second, superposing them one in top of the other (in the same place).

No problem -- then make an array of Icon or ArrayList<Icon> and stuff you ImageIcons in there. Regardless, my code above was not meant for cutting and pasting but to give you a general idea of the form of a solution, since the ideas can be extended to your problem. You will now need to take this idea as well as what you can glean from the Swing Timer tutorial (again the link can be found here), and try to write code for your own solution. Also please search this site on the subject of Swing Timer and animation because this kind of question gets asked a lot. But note when searching, don't look for exact duplicates of your question, since that usually doesn't exist, but rather questions that involve similar concepts, and then borrow the concepts showed in their solutions.

Best of luck in your endeavor!

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I don't have an array of ImageIcons, I have 13 images and a button, I want to make a external function (Out of the Button event) that makes appear 13 images in it every one second, superposing them one in top of the other (in the same place). I don't know if I'm making myself clear. Sorry for the inconvenience and thanks. – user3329320 Feb 19 '14 at 18:41
  • @user3329320: Please see edit to answer, and not to worry, there's been no inconvenience whatsoever. – Hovercraft Full Of Eels Feb 19 '14 at 19:55