1

I normally don't go on sites like these but I got a serious nooby question and can't really find out how to fix this.

So I have a simple dice that rolls when you hit a button. It prints out all the dice numbers in the console. Now I want to save the last int so I can do calculations etc with it. Now the problem is I have serious no idea how to get the last int of the dice and print it out in the console. Does anyone have experience in this and can help me? Here is my code:

btn.setText("Roll Die");
        btn.setOnAction((ActionEvent event) -> {
            btn.setDisable(true);//Disable Button
            Random random = new Random();
            int gekozen = Integer.parseInt(tf3.getText());

            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(.3), (actionEvent) -> {
                int tempRandom = random.nextInt(6) + 1;
                System.out.println(tempRandom);
                die.setDieFace(tempRandom);

        }));

        timeline.setCycleCount(random.nextInt(20) + 1);
            timeline.play();
            timeline.setOnFinished(actionEvent -> {
                btn.setDisable(false);//Enable Button

            });


});
  • Can't you just call `die.getDieFace()` in the `onFinished` handler? – James_D Apr 25 '18 at 20:35
  • @James_D No because that just gives me the image back – Javaprogrammern00b Apr 25 '18 at 20:39
  • It shouldn't. You are passing an `int` to `setDieFace(...)`, so you should get an `int` back if you call `getDieFace()` if your `Die` class is properly defined. You can always add another method to it that returns the `int` value if needed. – James_D Apr 25 '18 at 20:41
  • @James_D It returns: ImageView@1a808c06[styleClass=image-view] – Javaprogrammern00b Apr 25 '18 at 20:43
  • My code: timeline.setCycleCount(random.nextInt(20) + 1); timeline.play(); timeline.setOnFinished(actionEvent -> { btn.setDisable(false); System.out.println(die.getdieFace()); }); }); – Javaprogrammern00b Apr 25 '18 at 20:43
  • 2
    Sounds like you need to correct the design of whatever class `die` is. – James_D Apr 25 '18 at 20:45
  • 2
    Edit the question, don't place code in comment. Provide an [mcve]. Specifically, provide the Die class code and any other minimal code so that somebody could copy and paste your code to replicate the issue. – jewelsea Apr 25 '18 at 20:45
  • Why are you using a different account? https://stackoverflow.com/questions/50021161/java-how-to-update-dice-image-when-button-clicked-and-equal-to-number-given/50024369#50024369 – SedJ601 Apr 26 '18 at 03:27

2 Answers2

1

The best option is just to store the values somewhere. Your Die class (or whatever the class die is from) seems like a good candidate for that. In a well designed class, if you pass a value to a set method, say setXyz(x), then calling getXyz(), with no operations on the same object in the meantime, will return the same value x. So if you have designed your class following standard Java patterns, you can just do

timeline.setCycleCount(random.nextInt(20) + 1);
timeline.play();
timeline.setOnFinished(actionEvent -> {
    btn.setDisable(false);//Enable Button
    int dieValue = die.getDieFace();
    System.out.println(dieValue);
});

Another option is to compute all the temporary die values ahead of time:

btn.setText("Roll Die");
btn.setOnAction((ActionEvent event) -> {
    btn.setDisable(true);//Disable Button
    Random random = new Random();
    int gekozen = Integer.parseInt(tf3.getText());

    int numTempValues = random.nextInt(20) + 1 ;
    int[] tempValues = random.ints(1, 7).limit(numTempValues).toArray();
    int finalDieValue = tempValues[numTempValues - 1];
    Timeline timeline = new Timeline() ;
    for (int i = 0 ; i < tempValues.length ; i++) {
        KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.3).multiply(i+1), actionEvent -> {
            System.out.println(tempValues[i]);
            die.setDieFace(tempValues[i]);
        });
        timeline.getKeyFrames().add(keyFrame);
    }

    timeline.setOnFinished(actionEvent -> {
        btn.setDisable(false);//Enable Button
        System.out.println("You rolled: "+finalDieValue);
    });

    timeline.play();


});
James_D
  • 201,275
  • 16
  • 291
  • 322
0

You can add an int variable to the Die class.

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author blj0011
 */
public class Die
{
    ImageView dieFace;
    Image[] images;
    int dieValue;

    public Die(Image[] images)
    {
        this.images = images;
        dieFace = new ImageView(this.images[0]);//set default to image 0
        dieValue = 1;
    }

    public Die(Image[] images, int dieFaceValue)
    {
        //Need to catch for values less than 1 and greater than 6!
        this.images = images;
        dieFace = new ImageView(this.images[dieFaceValue - 1]);
        dieValue = dieFaceValue;
    }

    public ImageView getdieFace()
    {
        return dieFace;
    }

    public int getDieValue()
    {
        return dieValue;
    }

    public void setDieFace(int dieFaceValue)
    {
        //Need to catch for values less than 1 and greater than 6!
        dieFace.setImage(this.images[dieFaceValue - 1]);
        dieValue = dieFaceValue;
    }
} 
SedJ601
  • 12,173
  • 3
  • 41
  • 59