?? imageutil.java
字號:
package org.gamecollege.j2me.rpg;
import java.io.InputStream;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;
/**
* Bin compiler壓縮圖像讀取工具類
* @author Jagie
*
*/
public class ImageUtil {
//單例
public static final ImageUtil self = new ImageUtil();
//原始圖像文件名和其圖像數據在壓縮文件中的起始位置的映射表
private Hashtable fileTable;
//壓縮后的二進制文件名
private String binFile;
//私有構造函數
private ImageUtil() {
}
/**
* 單例的初始化方法,在使用ImageUtil的其他靜態(tài)方法之前,必須先執(zhí)行此靜態(tài)初始化方法。該只需執(zhí)行一次
* @param fileTable
* @param binFile
*/
public static void init(Hashtable fileTable,String binFile){
self.fileTable=fileTable;
self.binFile=binFile;
}
/**
* 根據圖片文件名,從壓縮文件中提取其圖像數據,構造Image對象
* @param pngFileName
* @return
*/
public static Image createImage(String pngFileName) {
Image result = null;
Long pos=(Long)self.fileTable.get(pngFileName);
if(pos!=null){
result=self.readImage(self.binFile,pos.longValue());
}
return result;
}
/**
* 根據二進制文件中的指定位置讀取圖像數據,構造Image對象。
* @param binfile
* @param pos
* @return
*/
private Image readImage(String binfile, long pos) {
byte buffer[];
int len;
try {
InputStream is = self.getClass().getResourceAsStream("/" + binfile);
is.skip(pos);
len = (is.read() & 0xFF) << 24;
len |= (is.read() & 0xFF) << 16;
len |= (is.read() & 0xFF) << 8;
len |= (is.read() & 0xFF);
/*
* 24 0xFF & 0xFF表示一個與,
* 轉成二進制就是這樣的 :
* 11111111 & 11111111 = 11111111,
*
* 再左移24位變成:
* 11111111000000000000000000000000
*/
buffer = new byte[len];
is.read(buffer, 0, buffer.length);
is.close();
is = null;
System.gc();
} catch (Exception e) {
buffer = null;
e.printStackTrace();
System.gc();
return null;
}
return Image.createImage(buffer, 0, buffer.length);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -