Load image from a file inside a project folder

Set the assets directory as a resource directory and then load the image as a resource from the location "/drawIcon.png":

URL url = getClass().getResource("/drawIcon.png");
Image image = ImageIO.read(url);

In case you want to create a javafx Image:

Image image = new Image("/drawIcon.png");

In this case, also, mark that folder as resource folder.

More info here: https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html


You can use getResource(path).toString(); the path must start with /, and it starts with the verry first package in your src folder.

Image img= new Image(getClass().getResource("/path/in/your/package/structure/icon.png").toString());

Tags:

Java

Javafx