将 Java 小程序转换为 Jar 时出现问题。可能是图片加载问题

发布于 2024-10-22 17:33:11 字数 756 浏览 1 评论 0原文

我正在eclipse中编写一个小程序,在eclipse环境下它运行良好。

当从该项目创建 jar 文件时,问题开始出现。

在使用多个选项测试 jar 后,我认为问题在于从网页加载图像。

小程序的任何其他功能似乎在 jar 中都可以正常工作。

我的项目中加载图像的代码如下所示:

MediaTracker mt = new MediaTracker(this);
String photo = imagePath
URL base = null;
 try { 
 base = getDocumentBase(); 
 } 
 catch (Exception e) {
}
 if(base == null){
      System.out.println("ERROR LOADING IMAGE");
 }
 Image imageBase = getImage(base,photo); 
// Some code that works on the image (not relevant)

   // The rest of the code
    icon.setImage(image);
   imageLabel.setIcon(icon);

但是 jar 无法加载 imgae,并且在运行时不会显示它,并且小程序因此被卡住。 (与 eclipse 不同,它加载图像并显示它)

可能是什么问题?

第二个问题是从 Eclipse 中的小程序加载需要几秒钟。有没有办法加快速度?

感谢您的帮助,

I'm writing an applet in eclipse and under the eclipse environment it works well.

while creating a jar file from this project, the problems start.

After testing the jar with several options, I think the problem is with loading an image from a web page.

Any Other features from the applet seems to work ok in the jar.

The code of loading image in my project looks like that:

MediaTracker mt = new MediaTracker(this);
String photo = imagePath
URL base = null;
 try { 
 base = getDocumentBase(); 
 } 
 catch (Exception e) {
}
 if(base == null){
      System.out.println("ERROR LOADING IMAGE");
 }
 Image imageBase = getImage(base,photo); 
// Some code that works on the image (not relevant)

   // The rest of the code
    icon.setImage(image);
   imageLabel.setIcon(icon);

But the jar can not load the imgae and it doesn't disply it in while running and the applet is stuck because of that. (unlike in the eclipse, which loads the image and shows it)

What could be the problem?

A second problem is that from the applet in the eclipse the loading take few seconds. Is there a way to speed things up?

Thanks for any help,

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

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

发布评论

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

评论(1

把时间冻结 2024-10-29 17:33:11

我不知道这在 Eclipse 中是如何工作的。

问题是 getDocumentBase() 返回嵌入小程序的页面位置(例如 http:// some.site.com/index.html),并且您正在尝试从该位置加载图片。显然,没有图片,只有一个html(或php)文件,加载失败。

如果您的目标是从 jar 内部加载图像,请尝试:

Image img = null;
try {
    img = ImageIO.read(getClass().getResource("/images/tree.png"));
} catch (IOException ex) {
    System.err.println("Picture loading failed!");
}

其中“/images/tree.png”是源树中图像文件的路径。

编辑:如果您只需要从 URL 加载图像,您可以使用:

Image img = null;
try {
    img = ImageIO.read(new URL("http://some.site.com/images/tree.png"));
} catch (IOException ex) {
    System.err.println("Picture loading failed!");
}

此方法比 Applet.getImage(new URL(...)) 好一点 - 我在加载时遇到了一些问题许多图像。

I have no idea how this could be working in Eclipse.

The problem is that getDocumentBase() returns location of a page, in which the applet is embedded (e.g. http://some.site.com/index.html), and you are trying to load a picture from that location. Obviously, there is no picture, just an html (or php) file, and the loading fails.

If your goal is to load an image from inside the jar, try:

Image img = null;
try {
    img = ImageIO.read(getClass().getResource("/images/tree.png"));
} catch (IOException ex) {
    System.err.println("Picture loading failed!");
}

where "/images/tree.png" is path to image file in your source tree.

EDIT: If you need just to load an image from URL, you can use:

Image img = null;
try {
    img = ImageIO.read(new URL("http://some.site.com/images/tree.png"));
} catch (IOException ex) {
    System.err.println("Picture loading failed!");
}

This method is a bit better than Applet.getImage(new URL(...)) - I had some problems when loading many images.

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