-1

While attempting to mess around with JavaFX and its basic functions on java 1.8 I kept running into the issue that my Image objects would not accept the URL for a png file contained within my src folder. The icon image object gave me trouble at first but when I copied the file name via the "Copy Path/Reference..." option it worked. This was not the case for the coinImage object where no matter how I format the image URL it will not accept it. To clarify, my files are named "icon.png" and "coin.png" and they are placed in the src folder. I have tried the following formats:

Image coinImage = new Image("coin.png");
Image coinImage = new Image("/coin.png");
Image coinImage = new Image("file:coin.png");
Image coinImage = new Image("file:/coin.png");

Below is the code which most relates to the problem to reduce on the size of this question:

public class MainStage extends Application {
    public void start(Stage stage) throws Exception{
        Group root = new Group();
        Scene scene = new Scene(root,1000,1000,Color.LIGHTSKYBLUE);
        Image icon = new Image("icon.png");

        Image coinImage = new Image("coin.png");
        ImageView imageView = new ImageView(coinImage);
        imageView.setX(400);
        imageView.setY(500);

        root.getChildren().add(imageView);
        stage.getIcons().add(icon);
        stage.setTitle("Test");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Here are all of my imports as well but I do not think they are the issue here:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

If any additional information is needed I should be able to provide it. Thanks.

Matthew
  • 41
  • 2
  • Refer to the [Eden resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/). Check the binary output of your build and ensure that the resource is actually in the location you are looking for it. Also see: [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other), examples are in the [Image javadoc](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/image/Image.html). – jewelsea Feb 05 '22 at 10:17
  • 1
    don't _mess around_ with random parameters, instead work through a decent tutorial on the basics (at the very least study the api doc of related classes throroughly - they might be a bit on the dense side, but digging into every single word/class you don't know yet will help :) – kleopatra Feb 05 '22 at 11:35

2 Answers2

0

It is recommended to keep images and resources in a dedicated assets folder.

You need to have the assets folder in your classpath.

Set the assets directory as a resource directory and then create a javafx Image from location: "/coinImage.png":

If you are using IntelliJ To set folder as resource directory follow this guide

Orr Benyamini
  • 395
  • 2
  • 9
-1

This issue is coming from the fact that Java will search for your image file in the bin directory. You should put your image in the bin directory.

I added a small print in your code that can help for debugging

System.out.println("GET  PATH"+this.getClass().getClassLoader().getResource("").getPath());

The output that i got

GET PATH /D:/Nabil-local/eclipse-workspace/Stackoverflow/bin/

I think there is another way to specify a folder

Nabil
  • 1,130
  • 5
  • 11