-4

Whenever I run it, it shows an error because it can't find the file.

This is with intelliJ, I've tried many solutions but they aren't working.

Someone replied before but I'm pretty sure I did their fix wrong because before it was showing a blank part where the image should be, but now it is giving me errors.

Library

package GUI;

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

import java.io.File;
import java.util.Objects;

public class GameGUI extends Application{
    @Override
    public void start(Stage primaryStage) {
        //Sets the stage
        //Stage Menu = new Stage();

        //Cheske Pic
        Image cheske = new Image(getClass().getResourceAsStream("src/main/resources/CheskePic.png"));
        ImageView cheskePic = new ImageView(cheske);
        cheskePic.setFitHeight(200);
        cheskePic.setFitWidth(200);

        //Choose Pieces Button
        Button Pieces = new Button("Choose Your Pieces");
        Pieces.setOnAction(e -> {
            //Edit the 2-D Array
        });

        //Start Game Button
        Button Start = new Button("Start");
        Start.setOnAction(e -> {
            //BoardGUI.show();
        });

        //Quit Game Button
        Button Quit = new Button("Quit");
        Quit.setOnAction(e -> {
            primaryStage.close();
        });

        //Sets the layout with all the buttons / pic
        VBox layout = new VBox(10);
        layout.getChildren().addAll(cheskePic,Pieces,Start,Quit);
        layout.setAlignment(Pos.CENTER);

        //Sets the scene with the layout we have given it
        Scene scene = new Scene(layout, 400, 400);

        //Sets all stage settings then shows the Menu
        primaryStage.setTitle("Hello!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args){
        launch();
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Aegent
  • 3
  • 1
  • 1
    please read (and edit the question accordingly) [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/a/285557/16320675) – user16320675 Jul 16 '22 at 17:14
  • Edit your question and include your code as text, not as images. – VGR Jul 16 '22 at 21:30
  • Voted to reopen. At the code now stands, you are attempting to load from a relative address (indicated by lack of starting "/") so the code is looking for a subdirectory "src/main/resources/..." from the directory where the GameGUI resides. There's a good chance if you use "/" at the beginning (which indicates start looking at the project root directory) you can use ("CheskePic.png") as your argument. I sometimes used to test the root location by writing a file to an adjacent address to see where it would turn up. Will move this to an answer if question reopens. – Phil Freihofner Jul 17 '22 at 17:49
  • Remove `src/main/resources`, use `/CheskePic.png`. Resources are **rooted** in the `resources` directory. The contents of the resource directory is copied to the root of your JAR, and therefor on the root of your classpath/modulepath. – Mark Rotteveel Jul 18 '22 at 10:44

1 Answers1

0

Firstly, please use the code option of stack overflow to share code, so that others can copy, edit and run the code in their IDE.

You have two options:

Bad way: Try to access the image by:

new Image("images/CheskePic.png");

Better way: Instead of saving the image in the src folder, create a resource folder and save the image there. IntelliJ should create such a folder automatically. If not create a folder named 'resources', right-click on it 'Mark Directory as' and click on 'Resource Root'

Then access the image by:

new Image(getClass().getResourceAsStream("path/to/image.png")));

or:

new Image(YourClass.class.getResourceAsStream("path/to/image.png")));
GregGott
  • 95
  • 9