0

I have spent the last 3 days trying to find an answer, watching videos and forum posts but no luck. Please take a look in those 4 pics. It seems that it only does it when the problem has image files.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;

public class Exercise14_02 extends Application {
    @Override
    public void start(Stage primaryStage) {
        Image imageX = new Image("image/x.gif");
        Image imageO = new Image("image/o.gif");

        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(5);
        pane.setVgap(5);

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int status = (int)(Math.random() * 3);
                if (status == 0) {
                    pane.add(new ImageView(imageX), j, i);     
                } else if (status == 1) {
                    pane.add(new ImageView(imageO), j, i);     
                }        
            }
        }

        // Create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setTitle("Exercise14_02"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public static void main(String[] args) {
        launch(args);
    }
}

error

pic2

pic3

pic4

0009laH
  • 1,960
  • 13
  • 27
Dino
  • 1

1 Answers1

2

try moving your images to 'resources' folder, create one if needed.

Image imagex = new Image(getClass().getResourceAsStream("/resources/your_image.jpg"));

or check if you have included your resources folder in project.

vinvin
  • 31
  • 4
  • having "resources" in the classpath name looks .. fishy (though it's possible to have a _package_ name containing it, it's unusual ;) – kleopatra Oct 29 '20 at 10:42