在Java小程序加载图像 [英] Loading Image in Java Applet

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

问题描述

当我尝试运行applet浏览器一个applet它无法找到资源(图像)。
我尝试加载这样的资源:

When I try to run an applet in applet viewer it is not able to find resources (Image). I try to load resource like this:

String cb= this.getCodeBase().toString();
String imgPath = cb+"com/blah/Images/a.png";
System.out.println("imgPath:"+imgPath);
java.net.URL imgURL = Applet.class.getResource(path);

但是当我运行它appet观众的路径是这样的:
imgPath:文件:D:/Work/app/build/classes/com/blah/Images/a.png

but when i run it in appet viewer path is like this: imgPath:file:D:/Work/app/build/classes/com/blah/Images/a.png

虽然形象有此路径,
为preFIX文件:造成问题,我怎么能测试这个code

though image is there in this path, is prefix file: causing problem, how can i test this code?

将在服务器和codeBase的部署时,此code工作返回服务器的URL?

Will this code work when deployed in server and codebase returns a server URL?

推荐答案

是您的小程序应该被加载后加载图像?或者你会得到更好的服务捆绑在罐子里需要的图像资源,您的小程序?

Is your applet supposed to load images after it is loaded? Or would you be better served bundling necessary image resources in the jar with your applet?

我的日常工作​​与丰富的GUI图形的小应用程序的应用程序。
他们被捆绑在jar文件。
这州市我们做什么:

I work daily on an applet-based application with plenty of graphics in the GUI. They are bundled in the jar-file. This si what we do:

// get the class of an object instance - any object.  
// We just defined an empty one, and did everything as static.
class EmptyClass{}
Class loadClass = new EmptyClass().getClass();
// load the image and put it directly into an ImageIcon if it suits you
ImageIcon ii = new ImageIcon(loadClass.getResource("/com/blah/Images/a.png"));
// and add the ImageIcon to your JComponent or JPanel in a JLabel
aComponent.add(new JLabel(ii));

请确保你的形象是actuallly在你认为它是罐子。
用途:

Make sure your image is actuallly in the jar where you think it is. Use:

罐子-tf< archive_file_name>

...得到的列表。

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

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