Get image path JavaFX

I want to get the pathname of the current image loaded in my image object

I have the following code:

Image lol = new Image("fxml/images/bilhar9.png");

What I want to do is as follows:

lol.getPath();

Should return "fxml / images / bilhar9. PNG", I found the method impl_ Geturl() is not recommended

What should I do?

Solution

You cannot get an image path from an image through an API that is not deprecated because such an API does not exist in Java 8 You can use deprecated APIs and risk damaging your application in future Java versions when deleting deprecated APIs - this is not desirable You can create a feature request to make geturl () a public API on the image, but there is no guarantee that it will be accepted. Even if it is, it can only be used in future Java versions

The image is not final, so I suggest the following:

class LocatedImage extends Image {
    private final String url;

    public LocatedImage(String url) {
        super(url);
        this.url = url;
    }

    public String getURL() {
        return url;
    }
}

Create your image like this:

Image image = new LocatedImage("fxml/images/bilhar9.png");

You can then access the web site in the following ways:

String url = image instanceof LocatedImage 
        ? ((LocatedImage) image).getURL() 
        : null;

Not a good solution, but it may be enough to meet your needs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>