Reputation: 23
I'm having some trouble with an assignment I'm working on.
Part of it is to "animate" the rolling of a pair of die. I have 6 JPEGS, one of each of the sides. Instead of rolling, I want it to cycle through the images. I'm trying to use setIcon over and over in a loop, and use Thread.sleep() for a delay. This, among other things, all happens when the user hits a button (the event). Instead of displaying the images, it waits the correct amount of time and displays only the last image. Here's the function I'm calling to perform the loop:
private void aniRoll(){
for(int i = 0; i < 10; i++){
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
try{
Thread.sleep(1000); // Sleep for 1 sec
}
catch(InterruptedException e){}
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face3.jpg")));
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face4.jpg")));
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face5.jpg")));
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face6.jpg")));
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
die1Disp is the name of my JLabel, aniRoll() is the function I'm calling to perform the loop (made private because I don't want anything outside of my GUI class to call it), and the faceX.jpg are my images.
What I can't understand is no matter where I put the Thread.sleep() it delays the program first, and then only displays the last image. For example:
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face1.jpg")));
die1Disp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/face2.jpg")));
//bunch of extra code
try{
Thread.sleep(10000); // Sleep for 10 sec
}
catch(InterruptedException e){}
This will wait for 10 seconds and then display only the second image. It doesn't blip the first quickly, display the second, then wait, it waits and only displays the second image. This quirk really has me baffled, and I'm not sure how to correct it. I'm using NetBeans IDE 7.01 as my editor and for the basic layout and code generation I'm using a Component Palette.
--Thanks
Upvotes: 2
Views: 160
Reputation: 5133
There's a single UI thread managing events and repainting (updating the screen). You put it to sleep. Doing so means that nothing else was going to happen (in regard to the UI) until it wakes up.
In your actionPerformed method, start a new Thread that will do the repainting. That should fix it.
Upvotes: 1
Reputation: 28568
First you must make the changes on the gui thread. See
SwingUtils.invokeLater.
Also Thread.sleep isn't going to cut it, you should be using a javax.swing.Timer to specify when to switch icons.
Upvotes: 3