I'm making a "dice game". I'm adding JLabels to a panel with random dice images. I want them to not appear at the same time, but with a delay. I found that Thread.sleep() does the job. The problem is that when I put the sleep() inside a for-loop, the loop loops and all of the labels are repainted in the same time (after the set sleep-amount). My goal is to show the first label, sleep for an amount of time, show the next, sleep and so on. Thanks for any help!
public void PaintLabels(Player[] players, Die[] dice, String[][] images) throws InterruptedException {
pnlDice.removeAll(); // Remove everything from the play-area
pnlDice.setLayout(new java.awt.GridLayout(players.length,6)); // Sets a new layout based on number of players
JLabel[] image = new JLabel[dice.length]; // Declares an array of labels based on number of dice
for (int i=0; i<players.length;i++) {
for (int j=0; j<dice.length; j++) {
image[j] = new JLabel();
image[j].setIcon(new ImageIcon(images[i][j])); // Set the image of the label
pnlDice.add(image[j]); // Add the label to the panel
Thread.sleep(200);
pnlDice.revalidate();
pnlDice.repaint();
}
}
}