Ivar Eriksson
Ivar Eriksson

Reputation: 973

JavaFX Cannot Find Image

just started a new project and am already running in to some issues. I am trying to display an image with javafx but the compiler is complaining that the provided location is invalid. Here is all of my code so far (apart from some imports):

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

    public void start(Stage primaryStage) throws Exception{
        Stage stage = primaryStage;
        String version = "alpha";
        stage.setTitle("Space Invaders v. " + version);

        VBox vbox = new VBox();
        Image pic = new Image("Art\\whiteMonster.jpg");
        ImageView image = new ImageView(pic);
        vbox.getChildren().add(image);

        Rectangle rect = new Rectangle(20,0,200,200);
        rect.setFill(Color.BLACK);
        vbox.getChildren().add(rect);

        //Creating the scene.
        Scene scene = new Scene(vbox);
        stage.setScene(scene);
        stage.show();
    }
}

If I change the file path to the images url where I found it in google image search it displays the black rectangle added beneath it (just to test if everything works) but not the image. In the current configuration however it simply throws an error saying the URL is either invalid or that the resource was not found. What makes me think that this should work is this example here: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html

Thanks for any help.

PS. I've been looking at other similar questions here but haven't found anything that helped me and if you feel like you need any other information give me a shout.

-----EDIT----- Project Structure, Code and File Manager, all images are placed directly in the "Art" folder Project Structure, Code and File Manager, all images are placed directly in the "Art" folder.

I hope that is all you need.

Upvotes: 0

Views: 4123

Answers (2)

Shibimiro
Shibimiro

Reputation: 1

The solution for me was to Close Project and open the Javafx Project again as shown here

This refreshes the images URL for Javafx.
NOTE: I am using Intellij IDEA Java version 16 with Javafx.

  • Error was: Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found

Upvotes: 0

Eixhani
Eixhani

Reputation: 60

Try putting the image inside the folder .Drag the image to the "src" and use Image pic = new Image("whiteMonster.jpg");. It should be displayed under the .java files but a little bit in the left.

Upvotes: 1

Related Questions