Search code examples
javajavafximageviewsimulatordice

Dice Simulator using JavaFX, images not appearing


I've been trying to create a Dice Simulator, where I will create a JavaFX application that simulates rolling a pair of dice. When the user clicks a button, my application will generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. My application will then display the dice by using ImageView controls.

Everything works fine except for the fact that my dice images won't appear. I'm not getting any errors either and the application runs fine. What am I doing wrong and why won't my dice images appear? Any help is greatly appreciated, thank you all.

DieRoll Class

import java.util.Random;

public class DieRoll 
{
    int rollNum;
    int sides = 6;
    
    public void roll()
    {
        Random rand = new Random();
        rollNum = rand.nextInt(sides) + 1;
    }
    
    public int getRoll()
    {
        return rollNum;
    }
}

DieImages Class

import javafx.scene.image.Image;

public class DieImages 
{
    Image pic1 = new Image("file:images/1Die.bmp");
    Image pic2 = new Image("file:images/2Die.bmp");
    Image pic3 = new Image("file:images/3Die.bmp");
    Image pic4 = new Image("file:images/4Die.bmp");
    Image pic5 = new Image("file:images/5Die.bmp");
    Image pic6 = new Image("file:images/6Die.bmp");
    
    private int value;
    private Image dieImage = pic1;
    
    public void setImage(int sides)
    {
        int value = sides;
        if(value == 1)
            dieImage = pic1;
        if(value == 2)
            dieImage = pic2;
        if(value == 3)
            dieImage = pic3;
        if(value == 4)
            dieImage = pic4;
        if(value == 5)
            dieImage = pic5;
        if(value == 6)
            dieImage = pic6;
    }
    
    public Image getImage()
    {
        return dieImage;
    }
    
}

DiceSimulator Class

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import java.util.Random;

public class DiceSimulator extends Application
{
     private ImageView viewDie1 = new ImageView();
     private ImageView viewDie2 = new ImageView();
     private Label resultDie;
     private Label message1;
     private Label message2;
     
     public static void main(String[] args)
       {
          // Launch the application.
          launch(args);
       }
       
       @Override
       public void start(Stage primaryStage)
       {
           primaryStage.setTitle("Dice Simulator");
           Label message1 = new Label("Welcome to the Dice Simulator!");
           Label message2 = new Label("Please hit start to roll the dice!");
           
           resultDie = new Label();
           Button startButton = new Button("Start!");
           
           startButton.setOnAction(new StartButtonHandler()); 
           
           HBox hbox = new HBox(10, viewDie1, viewDie2);
           
           VBox vbox = new VBox(10, message1, message2, startButton, hbox, resultDie);
           vbox.setPadding(new Insets(15));
           vbox.setAlignment(Pos.CENTER);
           
           Scene simulatorScene = new Scene(vbox);
           primaryStage.setScene(simulatorScene);
           primaryStage.show();
           
           
       }
       
       class StartButtonHandler implements EventHandler<ActionEvent>
       {
           @Override
           public void handle(ActionEvent event)
           {
               int num1 = 0;
               int num2 = 0;
               Image diePic1; 
               Image diePic2;

               DieRoll dieI = new DieRoll();
               DieRoll dieII = new DieRoll();
               dieI.roll();
               dieII.roll();
               num1 = dieI.getRoll();
               num2 = dieII.getRoll();
               
               DieImages image1 = new DieImages();
               DieImages image2 = new DieImages();
               image1.setImage(num1);
               image1.setImage(num2);
               diePic1 = image1.getImage();
               diePic2 = image2.getImage();
               
               viewDie1 = new ImageView(diePic1);
               viewDie2 = new ImageView(diePic2);
               
               resultDie.setText("You rolled a " + num1 + " and " + num2 + "!");
           }
       }
       
         
}


Solution

  • Java cannot find the files you are telling it to display, why JavaFX doesn't throw an error, no clue.

    Relative locations (like the ones you have specified right now) start from the java file if running the file by itself, or from the project root if running the file through an ide.

    For testing purposes, try to specify the file location using an absolute location such as /home/user/images/1Die.bmp for Linux or C:\Users\user\images\1Die.bmp for Windows