1

I have an application that is to launch a die, when you press the button to launch it, a random number is created 6 times (using a Thread.sleep ()) and must therefore display the face dice corresponding to the number, but here it shows me only the last digit, the others before do not appear. Here is my code:

private List listeFaceDice;

for(i=0;i<6;i++) {
        setRandomNum(rand.nextInt((6 - 1) + 1) + 1);
        imageView.setImage(listeFaceDice.get(randomNum-1)); 
        Thread.sleep(1000);
    }

with a TimeLine :

Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> {
        setRandomNum(rand.nextInt((6 - 1) + 1) + 1);
        imageView.setImage(listeFaceDice.get(randomNum-1));
        //System.out.println(getRandomNum());
    }));
    timeline.setCycleCount(6);
    timeline.play();
    timeline.setOnFinished(e -> {
        setRandomNum(randomNum);
    });

It works with the time LimeLine but I don't know how to get the value at the end of the animation, because I called this method when I press a button. Thank in advance ;)

Fapinski
  • 11
  • 3
  • Use a timeline. Also, note that a shorthand for `(6 - 1) + 1` is just `6`. You probably don't want to add one to the result, either, since the list indices are 0...5. – James_D Mar 15 '18 at 19:56
  • I already tried with a timeline, but I can not get the value of the die at the end of the timeline in another class. Here is the code of it: "Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> { setRandomNum(rand.nextInt((6 - 1) + 1) + 1); imageView.setImage(listeFaceDice.get(randomNum-1)); })); timeline.setCycleCount(6); timeline.play(); timeline.setOnFinished(e -> { setRandomNum(randomNum); }); " – Fapinski Mar 15 '18 at 20:09
  • Edit your question with the code using a timeline' – James_D Mar 15 '18 at 20:10
  • "I don't know how to get the value at the end of the animation": just do `timeline.setOnFinished(e -> {/* do whatever you need here with randomNum */});` – James_D Mar 15 '18 at 20:31

0 Answers0