如何确定我的javaFX应用程序正在从相对路径加载图像? [英] How can I be sure that my javaFX application is loading images from a relative path?

查看:47
本文介绍了如何确定我的javaFX应用程序正在从相对路径加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管使用了我认为是Java图像类中正确的文件路径的我也无法在游戏中加载图像.

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.

推荐答案

首先,图像应位于 src/main/resources 文件夹下.然后,您应该将它们作为资源加载

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

新图片(this.getClass().getResource("images/img.png").toExternalForm());

新图片("images/img.png")-API假定图片位于资源路径中

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

该路径应该相对于您用来调用 getResource 的类的相对路径.

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

从文件中加载它们会在组装应用程序后停止,这会将资源添加到jar中.

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

这篇关于如何确定我的javaFX应用程序正在从相对路径加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆