在导出的.jar文件中加载图像 [英] Loading images in the exported .jar file

查看:87
本文介绍了在导出的.jar文件中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我开始制作此Java游戏,并且一切正常,除非尝试将其导出为可运行的jar.当我尝试执行它时,它显示了一个空白屏幕.我发现这与我访问图像的方式有关,因此我尝试根据我在此处阅读的内容来更改代码,但仍然无法正常工作:[

So I started making this Java game and everything worked fine, except when I tried to export it as a runnable jar. It shows me a blank screen when I try to execute it. I found out that it has to do with the way I'm accessing the images so I tried to change the code according to what i read on here but it's still not working :[

我当前正在通过以下方式访问图像:

I'm currently accessing the images through:

Image image;
image = ImageIO.read(getClass().getResource("/imageName"));

也许你们可以帮助我,太好了!

Maybe you guys can help me, would be great!

这是我的包裹树:

推荐答案

为了将资源包括在导出的jar中,它们需要驻留在src文件夹中(否则,它将在当前目录中查找它们)运行jar的目录,该目录易于更改,需要单独提供(听起来像您想要的东西).

In order to include your resources inside your exported jar, they need to reside within the src folder (otherwise, it'll look for them in the current directory where the jar is run which is susceptible to change and would need to be supplied separately, which doesn't sound like what you want).

含义类似:

- src
  + com.liakos.SpaceShooter
  - res
    - 1.png
    - 2.png

以此类推.

然后您可以通过getResource API对其进行访问,即:

Then you can access it through the getResource API, i.e.:

Image image;
image = ImageIO.read(getClass().getResource("res/1.png"));

这篇关于在导出的.jar文件中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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