当前位置: 技术问答>java相关
Applet中装如图片,那种方式更好???
来源: 互联网 发布时间:2015-03-24
本文导语: 方式1. URL url = getCodeBase(); MemoImage = getImage(url, "img.jpg"); MediaTracker mt = new MediaTracker(this); mt.addImage(MemoImage, 1); 方式2. InputStream in = getClass().getResourceAsStream("img.jpg"); byte[] buffer = new byte[in.availa...
方式1.
URL url = getCodeBase();
MemoImage = getImage(url, "img.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(MemoImage, 1);
方式2.
InputStream in = getClass().getResourceAsStream("img.jpg");
byte[] buffer = new byte[in.available()];
in.read(buffer);
m_image = Toolkit.getDefaultToolkit().createImage(buffer);
-----------------------------------------------
那种方式更快,资源少??为什么?还有没有更好的方式.
URL url = getCodeBase();
MemoImage = getImage(url, "img.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(MemoImage, 1);
方式2.
InputStream in = getClass().getResourceAsStream("img.jpg");
byte[] buffer = new byte[in.available()];
in.read(buffer);
m_image = Toolkit.getDefaultToolkit().createImage(buffer);
-----------------------------------------------
那种方式更快,资源少??为什么?还有没有更好的方式.
|
Load an Image from a JAR file
[JDK1.1 application] String imgName = "AnImage.jpg";
URL imgURL = getClass().getResource(imgName);
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = null;
try {
MediaTracker m = new MediaTracker(this);
img = tk.getImage(imgURL);
m.addImage(img, 0);
m.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
[JDK 1.1 applet]
Because of some security reason, it's not possible with some browser (like Netscape) to use the getResource() method from an Applet. Instead we must use the getResourceAsStream method. try {
MediaTracker m = new MediaTracker(this);
InputStream is = getClass().getResourceAsStream("anImage.gif");
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bytes = new byte[10000];
int byteRead = bis.read(bytes,0,10000);
img = Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
m.addImage(img, 0);
m.waitForAll();
}
catch(Exception e) {
e.printStackTrace();
}
[JDK1.1 application] String imgName = "AnImage.jpg";
URL imgURL = getClass().getResource(imgName);
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = null;
try {
MediaTracker m = new MediaTracker(this);
img = tk.getImage(imgURL);
m.addImage(img, 0);
m.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
[JDK 1.1 applet]
Because of some security reason, it's not possible with some browser (like Netscape) to use the getResource() method from an Applet. Instead we must use the getResourceAsStream method. try {
MediaTracker m = new MediaTracker(this);
InputStream is = getClass().getResourceAsStream("anImage.gif");
BufferedInputStream bis = new BufferedInputStream(is);
byte[] bytes = new byte[10000];
int byteRead = bis.read(bytes,0,10000);
img = Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
m.addImage(img, 0);
m.waitForAll();
}
catch(Exception e) {
e.printStackTrace();
}