在Android上工作了1年後,我在傳統Java GUI中顯得有點生疏。JApplets:加載圖片
我需要知道在這樣兩件事情,我打開圖像
但首先一些代碼
/**
* Load the image for the specified frame of animation. Since
* this runs as an applet, we use getResourceAsStream for
* efficiency and so it'll work in older versions of Java Plug-in.
*/
protected ImageIcon loadImage(String imageNum, String extension) {
String path = dir + "/" + imageNum+"."+extension;
int MAX_IMAGE_SIZE = 2400000; //Change this to the size of
//your biggest image, in bytes.
int count = 0;
BufferedInputStream imgStream = new BufferedInputStream(
this.getClass().getResourceAsStream(path));
if (imgStream != null) {
byte buf[] = new byte[MAX_IMAGE_SIZE];
try {
count = imgStream.read(buf);
imgStream.close();
} catch (java.io.IOException ioe) {
System.err.println("Couldn't read stream from file: " + path);
return null;
}
if (count <= 0) {
System.err.println("Empty file: " + path);
return null;
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
我這樣稱呼它
loadImage("share_back_img_1_512", "jpg");
我的問題是:如何使它更具活力。
目前我正在測試一些圖像,但我有像最終的小程序100圖像的東西。
我不得不將圖像存儲在一個包中以便能夠訪問它們。
所以這裏的問題是:
有沒有一種方法取決於包的內容載入圖像? 獲取名稱,大小,擴展名...?
基本上是一個簡單的生成ImageIcons