简体   繁体   English

我怎么能确定我的javaFX应用程序是从相对路径加载图像?

[英]How can I be sure that my javaFX application is loading images from a relative path?

I'm unable to load images in my game despite using what I thought was the proper file path in the java image class. 尽管使用了我认为是java图像类中的正确文件路径,但我无法在游戏中加载图像。

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. 任何帮助是极大的赞赏。

First of all, images should be under the src/main/resources folder. 首先,图像应位于src/main/resources文件夹下。 Then you should load them as resources 然后你应该把它们作为资源加载

new Image(this.getClass().getResource("images/img.png").toExternalForm()); or 要么

new Image("images/img.png") - the API assumes the image is in the resource path new Image("images/img.png") - API假定图像位于资源路径中

The path should be relative to the class you use to call getResource on. 该路径应该相对于用于调用getResource的类。

Loading them from files will brake after assembling your application, which adds resource as part of the jar. 从文件中加载它们会在组装应用程序后制动,这会将资源添加为jar的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM