Ivar Eriksson Ivar Eriksson - 2 years ago 113
Java Question

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) {

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);

Rectangle rect = new Rectangle(20,0,200,200);

//Creating the scene.
Scene scene = new Scene(vbox);

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.

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download