Determine relative path of an image in Java fx

I know this has been asked many times and I have searched all over the world for a solution to this probably simple problem. I am trying to follow the simple javaFX tutorials on the Oracle website. I can define an image this way:

Image img = new Image("images/portal.png", 50, 50, true, true);

      

This works when the image is in a folder inside the "src" folder, but I am trying to find it to find the image when I have a folder with images outside the "src" folder, for example:

http://puu.sh/drF7K/bbf1a047aa.png

How can I make this work? All I get are errors saying "Invalid URL or resource not found". I tried using an absolute path, tried putting ".." on it, tried "HS-Graph / images / portal.png" and everything in between :( Thanks!

+3


source to share


3 answers


I'm going to answer my own question as I actually found a solution for this! My solution is to use the "file:" prefix when specifying the path. So:

Image img = new Image("file:images/portal.png");

      



Works great when the image file is outside my src folder !

+12


source


I think you are in trouble because the Pictures folder is outside the scope of your project. You may consider changing the structure of your project.

Example:



->src
|-->main
    |--->java
          |-->(default package)
    |--->resources
          |-->images

      

Then you should be able to access your image using the path. / Src / main / resources / images / portal.png

+1


source


Let me try to put it this way; If you want to use an image found in another directory, it's better to specify its relative path. An example if I was looking for a portal image in my ImageFile. I will do the following:

Image img = new Image("imagefile/portal.png");

      

0


source







All Articles