1

I have this project and i have to implement a game. The game is Yahtzee and I need the picture to change after a dice is rolled. So when i call the function and I get a one, the dice image should change to another one I saved as diceFace1 (f.e.). Here is one of the images as it pre-loads

<ImageView fx:id="dice1" fitHeight="66.0" fitWidth="75.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true">
<HBox.margin>
<Insets left="10.0" right="30.0" top="20.0" />
</HBox.margin>
<image>
<Image url="@../Dices/beforeRollDice.png" fx:id="dice1img"/>
</image>
</ImageView>

And here is a blueprint of the function:

public void rollDice() {
        Dice Dice = new Dice();
        int diceVal = Dice.roll();
        File imgPath = new File("@../Dices/Side1.jpeg");
        System.out.println(diceVal);
        switch (diceVal) {
            case 1:
                dice1.setImage(new Image(imgPath.toURI().toString()));
                break;
            case 2:
                //dice1.setImage(new Image(imgPath.toURI().toString()));
                break;
            case 3:
                //dice1.setImage(new Image(imgPath.toURI().toString()));
                break;
            case 4:
                //dice1.setImage(new Image(imgPath.toURI().toString()));
                break;
            case 5:
                //dice1.setImage(new Image(imgPath.toURI().toString()));
                break;
            case 6:
                //dice1.setImage(new Image(imgPath.toURI().toString()));
                break;

        }

    }
}

Problem is that when I call the function, the new image wont appear and the old one goes blank.

Harsh
  • 350
  • 1
  • 4
  • 13
  • https://github.com/sedj601/JavaFXDieExample – SedJ601 May 17 '20 at 00:55
  • 3
    The “@“ notation is specific to location URLs in FXML files. The image here is part of the application, so it is a resource, not a file. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other for how to get the correct resource path. – James_D May 17 '20 at 01:30
  • I chose this duplicate since you specifically requested to load the image from a file. I agree with James_D though: Such images should be included as resources instead. – fabian May 17 '20 at 06:49

0 Answers0