0

I have a blackjack program and I want to show the cards with 1 second delay. When I use Thread.sleep(1000) it waits and shows all of the cards at the same time.

    do {
        neueKarte(new ActionEvent());
        Thread.sleep(1000);
    } while (bot.getValue() <= 16);

shows all the images after finishing with the loop, not every second

    @FXML private void neueKarte(ActionEvent event) throws Exception {
    if(spielerDran == true) {
        sp1.nimmKarte(deck, 1);
        punkte.setText("Deine Punkte: " + Integer.toString(sp1.getValue()));
        naechstesFeld().setImage(sp1.hand.get(sp1.hand.size()-1).getImage());
        if(sp1.hand.size()==2) {
            aussteigen.setDisable(false);
        }
        if(sp1.getValue()>=21) {
            zieheKarte.setDisable(true);
            aussteigen(new ActionEvent());
        }
    } else {
        bot.nimmKarte(deck, 1);
        botPunkte.setText("Computer Punkte: " + Integer.toString(bot.getValue()));
        naechstesFeld().setImage(bot.hand.get(bot.hand.size()-1).getImage());
    }
}
Kai
  • 5,850
  • 13
  • 43
  • 63
eric
  • 3
  • 1

1 Answers1

-1

Use the Timer scheduled, here an example:

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    // do the job here
                    if (bot.getValue() <= 16) {
                        neueKarte(new ActionEvent());
                    } else {
                        cancel();
                    } 
                }
            });
        }
    }, 0, 1000);
Husam
  • 2,069
  • 3
  • 19
  • 29
  • Don't use `Timer` or `SwingUtilities.invokeLater(...)` for JavaFX applications: you will be updating the UI on the wrong thread. Use the JavaFX [concurrent API](http://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/package-frame.html) or [animation API](http://docs.oracle.com/javase/8/javafx/api/javafx/animation/package-frame.html) instead. – James_D Nov 02 '15 at 16:32
  • @James_D Thanks for your hint :) – Husam Nov 02 '15 at 18:27