Tony George
Tony George

Reputation: 75

can't loading image in Gluon javafx

I have a problem while trying to add image in my scene in the javafx gluon application using the line bellow I put the img.jpg file in the pin folder

Imageview image = new ImageView(new Image("file: img.jpg"));

and also using ("/img.jpg") any suggestions ?

Upvotes: 1

Views: 521

Answers (1)

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17359

Your image should be stored under src/main/resources folder according to a Gradle project structure. Usually it should be under the same package as your class, in this case the following code will load the image:

Image image = new Image(YourClass.class.getResource("img.jpg").toExternalForm());
ImageView imageView  = new ImageView(image);

More information about loading resources in Java can be found at https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

Upvotes: 1

Related Questions