I have this black jack game that I've been working on and I want the game to pause before it shows the next card like the dealer is dealing it. I have a method called dealersTurn that sorts out the dealers turn
public static void dealersTurn() throws InterruptedException {
if(game.getDealerHand().hasAce()){
while(!game.getDealerHand().didBust() && game.getDealerHand().getTotal() +10 < 17){
Thread.sleep(2000);
game.dealCard(game.getDealerHand());
view.updateDealerPanel();
}
while(!game.getDealerHand().didBust() && game.getDealerHand().getTotal() + 10 > 21 && game.getDealerHand().getTotal() < 17){
Thread.sleep(2000);
game.dealCard(game.getDealerHand());
view.updateDealerPanel();
}
}
else {
while(!game.getDealerHand().didBust()&& game.getDealerHand().getTotal() < 17){
Thread.sleep(2000);
game.dealCard(game.getDealerHand());
view.updateDealerPanel();
}
}
}
I tried using thread.sleep but instead of pausing in between dealing cards it pauses for what seems like the total amount of time that was paused for all the cards and then it shows all of the cards that were dealt all at once.
I thought it might be somethig with the view.updateDealerPanel but I can't figure it out
public void updateDealerPanel(){
dealerPanel.setVisible(false);
dealerPanel.removeAll();
dealerPanel.setLayout(new FlowLayout());
for(Card card : game.getDealerHand().getCards()){
JLabel cardPic = new JLabel(new ImageIcon(card.getImg()));
dealerPanel.add(cardPic);
}
JLabel tot = new JLabel("Total: " + game.getDealerHand().getTotalString());
dealerPanel.add(tot);
dealerPanel.setVisible(true);
}
please help thank you
Like @MadProgrammer commented, using a Swing Timer is probably the best way to go about your problem.
Here's how you could add a Swing Timer to your code, as well as simplifying all those while loops:
though, another option, and the one I thought to first, was to use
SwingWorkerfor background processing, alongsideSwingUtilities.invokeLater().This option would look something like this:
as you can see, this option is a lot uglier, and bloated compared to just using a Swing Timer. I just added it in case you wanted a different choice from the Timer :)