0

Working on a Dice Simulator using JavaFX and everything is working perfectly, however the actual Die image corresponding to each number isn't showing. I did copy the image path but nothing shows up when testing it out, even tried importing the images into the package to call it together using Image pic1 = new Image("file: Die1.bmp"); but no luck , so I'm starting to think it another problem with perhaps by variable names or method? Here is my code:

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

public class TaskB extends Application {
    private ImageView viewDie1=new ImageView();
    private ImageView viewDie2=new ImageView();
    private Label resultRoll;
    private Label intro;
    private Label begin;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Dice Simulator");
        intro=new Label("Welcome to the Dice Simulator!");
        begin=new Label("Click \"Roll!\" to roll a pair of dice");

        resultRoll=new Label();
        Button startRoll=new Button("Roll!");

        startRoll.setOnAction(new StartButtonHandler());
        HBox hbox=new HBox(10, viewDie1, viewDie2);

        VBox vbox=new VBox(20,intro, begin, startRoll, hbox, resultRoll);
        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 dieImage1;
                    Image dieImage2;

                    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);
                    image2.setImage(num2);
                    dieImage1 = image1.getImage();
                    dieImage2 = image2.getImage();

                    viewDie1 = new ImageView(dieImage1);
                    viewDie2 = new ImageView(dieImage2);

                    resultRoll.setText("You rolled a " + num1 + " and " + num2 + "!");
           }
        }
        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;
            }
        }
        public class DieImages {
            Image pic1 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die1.bmp");
            Image pic2 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die2.bmp");
            Image pic3 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die3.bmp");
            Image pic4 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die4.bmp");
            Image pic5 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die5.bmp");
            Image pic6 = new Image("file: C:\\Users\\sandr\\Documents\\NetBeansProjects\\TaskB\\Die\\Die6.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;
            }
        }
    public static void main(String[] args) {
        launch(args);
    }
}
  • It could be the ' ' in the between `file:` and the actual path. Create a `File` instance from the path and then use the URL to construct the `Image`. That way you can check if the file exists. – hotzst Feb 29 '20 at 06:59
  • @hotzst How do I use the URL to construct the image if the image is in my computer file? This is my first JavaFX project so I'm still trying to understand the logistics. – SaucyMelon Feb 29 '20 at 07:14
  • It is the same constructor (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html#Image-java.lang.String-) but the URL is not created by hand but through the File instance thereby verifying that it has a valid format. – hotzst Feb 29 '20 at 07:33
  • 1
    I strongly recommend not accessing the images via file system. Otherwise the app will fail to find those images on another machine, unless it has those images stored in the exact same location as you store them on your current machine. Chances are that NetBeans isn't even installed there... (this is even ignoring the fact that you could forget about porting this to linux). Store those files in the classpath at runtime (e.g. include them in your jar) and use `Class.getResource(resourcePath)` to get an url for the image. – fabian Feb 29 '20 at 08:49

0 Answers0