一旦出口,JAVA找不到/绘制图像 [英] Once exported, java cannot find/draw images
问题描述
您好stackoverflowites,
Hello stackoverflowites,
我在开发2D游戏的过程中,当我运行Eclipse中,我的程序加载它的所有图像和资源的罚款。不过,我使用创建我的图片和图像图标的未必然,推荐的方式,这是如下图所示:
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));
这没有任何工作,我得到一个未捕获错误获取图像:跟踪,刚刚告诉我,我的网址(imgUrl的)为空。 (这是当我的Eclipse内运行,你要知道,我甚至没有带出口它尚未)
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.
在Eclipse中,我的包结构如下:
Inside of eclipse, my package structure is as follows:
(父目录,项目名称)
+ src文件夹,有正常的包等,构建路径
+src folder, has normal packages, etc, on build path
+资源文件夹,(RES),而不是构建路径(试过构建路径上,没有什么变化)
+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.
推荐答案
您code最初寻找图像文件和文件不JAR文件,只是资源的内部存在。所以,你是正确的使用 getClass.getResource(/ RES /客户/ BannerHeader.jpg)尝试;
。但要确保图像实际上是在一个目录类文件目录的jar文件,并使用相对于类路径目录的路径。
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.
该错误不告诉你,这是错误的,或者该文件是空的,而是你需要把这个在try / catch块。我建议你阅读例外教程了解这一点。
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.
修改1 结果
当你说你有一个未捕获的异常错误,在我看来,你有一个编译错误,而不是一个运行时异常。很抱歉的混乱。
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.
至于在何处放置的图像,它一定程度上取决于你所使用的IDE。我使用Eclipse,我加了,我称之为图像,并把我的图片有我的Java文件包目录的目录。然后我用资源来寻找图像/ myImage.jpg这个参数。
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".
举例来说,假设我在Eclipse包看起来像这样:
For example, say my packages in Eclipse look like so:
因此,类文件位于MyPackage的包中,并把影像文件,GridBoxClassPic.JPG位于图像关闭目录类文件的目录,这样我就可以使用String 字符串找到它RESOURCE_PATH =图像/ GridBoxClassPic.JPG;
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";
.
这code可以显示图片:
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();
}
}
这篇关于一旦出口,JAVA找不到/绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!