简体   繁体   English

将Java Applet转换为Jar时出现问题。 也许图片加载问题

[英]Problem converting Java applet to Jar. Maybe Image loading problem

I'm writing an applet in eclipse and under the eclipse environment it works well. 我正在eclipse中编写一个applet,在eclipse环境下它运行良好。

while creating a jar file from this project, the problems start. 从该项目创建jar文件时,问题开始了。

After testing the jar with several options, I think the problem is with loading an image from a web page. 在对jar进行了几个选项测试之后,我认为问题在于从网页加载图像。

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. 但是该jar无法加载imgae,并且在运行时不会将其显示在其中,因此applet被卡住了。 (unlike in the eclipse, which loads the image and shows it) (与在Eclipse中加载图像并显示图像不同)

What could be the problem? 可能是什么问题呢?

A second problem is that from the applet in the eclipse the loading take few seconds. 第二个问题是从Eclipse的applet加载需要花费几秒钟的时间。 Is there a way to speed things up? 有办法加快速度吗?

Thanks for any help, 谢谢你的帮助,

I have no idea how this could be working in Eclipse. 我不知道这在Eclipse中如何工作。

The problem is that getDocumentBase() returns location of a page, in which the applet is embedded (eg http://some.site.com/index.html ), and you are trying to load a picture from that location. 问题在于getDocumentBase()返回嵌入了applet的页面的位置(例如, http : //some.site.com/index.html ),并且您试图从该位置加载图片。 Obviously, there is no picture, just an html (or php) file, and the loading fails. 显然,没有图片,只有一个html(或php)文件,加载失败。

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. 其中“ /images/tree.png”是源树中图像文件的路径。

EDIT : If you need just to load an image from URL, you can use: 编辑 :如果您只需要从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!");
}

This method is a bit better than Applet.getImage(new URL(...)) - I had some problems when loading many images. 此方法比Applet.getImage(new URL(...))更好-加载许多图像时遇到一些问题。

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

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