⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageutil.java.bak

📁 游戏学院《天下武王》教学实例
💻 BAK
字号:
//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的其他静态方法之前,必须先执行此静态初始化方法。该只需执行一次
     * @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;
       
    try{
    	 Image.createImage(pngFileName);
    	
    	
    	}catch(Exception e)
    {}
    	  return result;
    
    	
//    	
//        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);

            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 + -