I'm creating an animation using JLabel,
public void updateLabels() {
label.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageCALA,300,300)));
label_1.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageStates,300,300)));
label_2.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageStrategies,300,300)));
label_3.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imagekD,300,300)));
currentIndexLabel++;
}
and I have a button that updates the labels
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
while (currentIndexLabel != paint.length-1) {
updateLabels();
}
}
});
But, I don't know how to wait, say for example 1000ms, until next change. When I add this:
try { Thread.sleep(1000); } catch (Exception e){}
into my ActionListener:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
while (currentIndexLabel!=paint.length-1) {
updateLabels();
try { Thread.sleep(1000); } catch (Exception e){}
}
}
});
its not working. It just stopping for a while and I don't see changes between the first and last frames. Is it possibile to Wait 1000ms without stopping the program? When I delete the while loop and try section, and click my button it is changing pretty well...
How can I do this?