导出后,java无法找到/绘制图像

发布于 2024-12-10 06:11:50 字数 796 浏览 0 评论 0原文

你好 stackoverflowites,

我正在开发一个 2d 游戏,当我在 eclipse 中运行它时,我的程序加载了它的所有图像和资源。但是,我正在使用不一定推荐的方式来创建图像和图像图标,如下所示:

bannerLogo.setIcon(new ImageIcon("/res/client/BannerHeader.jpg"));

现在,当导出到 jar 时,它不会显示图像(这是预期的,因为我没有使用正确的方式。)

我搜索了正确的方法,我发现是:

URL imgURL = getClass().getResource("/res/client/BannerHeader.jpg");
Image bannerImg = Toolkit.getDefaultToolkit().getImage(imgURL);
bannerLogo.setIcon(new ImageIcon(bannerImg));

这也不起作用,我得到一个“未捕获的错误获取图像:”跟踪,它只是告诉我我的 URL (imgURL) 为空。 (这是当我在 Eclipse 中运行时,请注意,我什至还没有导出它)

我认为它与我的类路径有关,但是我不知道是什么。

在eclipse内部,我的包结构如下:(

父目录,项目名称)

+src文件夹,在构建路径+资源文件夹(“res”)上有普通包等

,而不是在构建路径上(在构建上尝试过)路径,没有任何改变)

+++资源文件夹的子目录

我不知道在这里做什么。抱歉,文字墙。

Hello stackoverflowites,

I am in the process of developing a 2d game, and when I run it inside of eclipse, my program loads all of it's images and resources fine. However, I am using the not-necessarily-recommended way of creating my images and image icons, which is shown below:

bannerLogo.setIcon(new ImageIcon("/res/client/BannerHeader.jpg"));

Now, when exporting to a jar, it does not show the image (which is expected since im not using the correct way.)

I searched for the correct way to do it, which I found was:

URL imgURL = getClass().getResource("/res/client/BannerHeader.jpg");
Image bannerImg = Toolkit.getDefaultToolkit().getImage(imgURL);
bannerLogo.setIcon(new ImageIcon(bannerImg));

And that didn't work either, I get an "Uncaught error fetching image: " trace, which just tells me that my URL (imgURL) is null. (This is when I run inside of eclipse, mind you, I havn't even exported it yet)

I figure it has something to do with my classpath,however I cannot figure out what.

Inside of eclipse, my package structure is as follows:

(Parent Directory, project name)

+src folder, has normal packages, etc, on build path

+resource folder, ("res"), not on build path (tried it on build path, nothing changed)

+++subdirectories of resource folder

Im at a loss of what to do here guys. Sorry for the wall of text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-12-17 06:11:50

您的代码最初是在查找图像文件,而 Jar 文件中不存在文件,而只是资源。因此,您尝试使用 getClass.getResource("/res/client/BannerHeader.jpg"); 是正确的。但请确保该映像实际上位于类文件目录之外的目录中的 jar 文件中,并使用相对于类路径目录的路径。

该错误并不是告诉您这是错误的或文件为空,而是您需要将其放在 try/catch 块中。我建议您阅读异常教程以获取更多相关信息。

编辑 1
当你说你有一个未捕获的异常错误时,在我看来你有一个编译错误,而不是运行时异常。抱歉造成混乱。

至于放置图像的位置,在某种程度上取决于您使用的 IDE。我使用 Eclipse,并在 java 文件包目录中添加一个目录,我将其称为图像并将图像放置在那里。然后我使用资源来查找“images/MyImage.jpg”。

例如,假设我在 Eclipse 中的包如下所示:

Eclipse Package Explorer Image

因此类文件位于 myPackage 包中,图像文件 GridBoxClassPic.JPG 位于类文件目录之外的 images 目录中,因此我可以使用 String String RESOURCE_PATH = 找到它“图像/GridBoxClassPic.JPG”;

这段代码可以显示图像:

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class MyClass {
   private static final String RESOURCE_PATH = "images/GridBoxClassPic.JPG";

   public MyClass() {
      try {
         BufferedImage image = ImageIO.read(getClass().getResource(RESOURCE_PATH));
         ImageIcon icon = new ImageIcon(image);
         JLabel label = new JLabel(icon);
         JOptionPane.showMessageDialog(null, label);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

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

Your code was initially looking for image files, and files do not exist inside of a Jar file, just resources. So you were correct to try using getClass.getResource("/res/client/BannerHeader.jpg");. But make sure that the image is in fact in the jar file in a directory off of the class file directory, and use a path that is relative to the class path directory.

The error isn't telling you that this is wrong or that the file is null, but rather you need to place this in a try/catch block. I suggest that you read the Exceptions Tutorial for more on this.

Edit 1
When you stated you had an uncaught exception error, it seemed to me that you had a compilation error, not a run-time exception. Sorry for the confusion.

As for where to place the images, it somewhat depends on what IDE you're using. I use Eclipse, and I add a directory off of my java file package directory that I call images and place my images there. I then use resources to look for "images/MyImage.jpg".

For example, say my packages in Eclipse look like so:

Eclipse Package Explorer Image

So the class files are located in the myPackage package, and the image file, GridBoxClassPic.JPG is located in the images directory off of the class file directory, so I can find it using the String String RESOURCE_PATH = "images/GridBoxClassPic.JPG";.

This code could show the image:

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class MyClass {
   private static final String RESOURCE_PATH = "images/GridBoxClassPic.JPG";

   public MyClass() {
      try {
         BufferedImage image = ImageIO.read(getClass().getResource(RESOURCE_PATH));
         ImageIcon icon = new ImageIcon(image);
         JLabel label = new JLabel(icon);
         JOptionPane.showMessageDialog(null, label);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      new MyClass();
   }
}
烈酒灼喉 2024-12-17 06:11:50

好吧,如果您遵循方法一,我认为您只需要在 eclipse 中创建可运行的 jar 文件。然后,您需要将要加载的图像放在与 jar 文件相同的目录中,并具有与您在程序中提到的相同的相对路径。例如,

 your_directory
             ---> game_runnable.jar
             ---> res
                  ---> client 
                       -->BannerHeader.png

我认为那时它会起作用。

well, if you followed method one, I think you need to just create the runnable jar file in eclipse. Then you need to put the images you are loading in the same directory as the jar file, having the same relative path you had mentioned in your program. For example

 your_directory
             ---> game_runnable.jar
             ---> res
                  ---> client 
                       -->BannerHeader.png

I think it will work then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文