1

I'm unable to load images in my game despite using what I thought was the proper file path in the java image class.

I've multiple file paths and extensions available, and have messed around with the file location as well, the current file structure is:

 --src

   -- main.java

   -- images

     -- the image
Image earth = new Image("file:projectName/images/img.png");

I expect it to show up once I launch my game, but it unexpectedly doesn't show up, and the game doesn't crash neither. Any help is greatly appreciated.

eHayik
  • 2,981
  • 1
  • 21
  • 33

1 Answers1

3

First of all, images should be under the src/main/resources folder. Then you should load them as resources

new Image(this.getClass().getResource("images/img.png").toExternalForm()); or

new Image("images/img.png") - the API assumes the image is in the resource path

The path should be relative to the class you use to call getResource on.

Loading them from files will brake after assembling your application, which adds resource as part of the jar.

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
  • When I use this I am told that I cannot convert a java.net.URL to javafx.scene.Scene.image . Should I wrap the code you gave in anything? – Neil Estrada Aug 29 '19 at 16:49
  • 2
    getResource gives you correct URL, but then you use the image with it: new Image(getClass().getResource("resources/icon.gif")); I also updated the answer – Eugene Ryzhikov Aug 29 '19 at 17:04
  • 3
    @NeilEstrada The JavaFX [`Image`](https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/image/Image.html) class has no constructor that accepts a `URL`—it expects URLs to be in `String` form. Just call `toString()` or `toExternalForm()` on the `URL` you get from `getResource(...)`. That said, you should be able to simply pass `"images/img.png"` since `Image` interprets scheme-less URLs to be relative to the classpath (if using Jigsaw modules this can be slightly more complicated). – Slaw Aug 29 '19 at 17:16
  • @Eugene Ryzhikov Thanks so much for your continued help. I am now getting an error that says no suitable constructor found for Image, any clue why this is occuring? – Neil Estrada Aug 29 '19 at 17:19
  • Also, why do I not need to include resources in the file path? – Neil Estrada Aug 29 '19 at 17:32
  • 1
    It is possible for resources to be external for the application, but more commonly, static resources are packaged together with the it. That means they are part of the jar in a final form, and that is not the same as loading them from the file directly. So even though it may work in IDE - it will not work after deployment – Eugene Ryzhikov Aug 29 '19 at 19:06
  • 3
    `src/main/resources` is a Maven convention. If a different tool is being used to build, the images can be in a different location, as long as the build packages them from there or includes their location in its runtime classpath. (I’m sure you know this, but I’m adding it for the benefit of Neil and other readers.) – VGR Aug 29 '19 at 19:12
  • 2
    Now days, Maven convention is what most tools in Java world use I think – Eugene Ryzhikov Aug 29 '19 at 19:25