tools.java

来自「游戏学院《天下武王》教学实例」· Java 代码 · 共 300 行

JAVA
300
字号
//package org.gamecollege.j2me.rpg;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.game.Sprite;

/**
 * @author Jagie 工具方法集合类
 */
public class Tools {
    /**
     * 单例
     */
    private static final Tools self = new Tools();

    /**
     * 私有构造函数。
     *
     */
    private Tools() {

    }

    /**
     * 以字符的方式读取文件
     *
     * @param fichero
     *            文件名
     * @return 整个文件形成的字符串
     */
    public final static String readFileToString(String fichero) {
        StringBuffer str = new StringBuffer();
        try {
            Class clase = self.getClass();
            InputStream is;
            is = clase.getResourceAsStream(fichero);
            InputStreamReader isr = new InputStreamReader(is);
            char[] chars = new char[1];

            while (isr.read(chars) != -1) {
                str.append(new String(chars));
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException ficheroNoExiste) {
            return "";
        }
        return str.toString();

    }


    /**
     * 以字符的方式读取文件
     *
     * @param fichero
     *            文件名
     * @return 整个文件形成的字符串
     */
    public final static String read8859FileToString(String fichero) {
        StringBuffer str = new StringBuffer();
        try {
            Class clase = self.getClass();
            InputStream is;
            is = clase.getResourceAsStream(fichero);
            InputStreamReader isr = new InputStreamReader(is, "ISO8859_1");
            int ch;
            while ((ch = isr.read()) > -1) {
                str.append((char) ch);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException ficheroNoExiste) {
            return "";
        }
        return str.toString();

    }

    /**
     * 读取UTF编码的文件,形成字符串
     *
     * @param fn
     * @return
     */
    public final static String readUTFFileToString(String fn) {
        StringBuffer sb = new StringBuffer();
        try {
            Class clase = self.getClass();
            InputStream is;
            is = clase.getResourceAsStream(fn);

            byte[] bytes = new byte[1];

            while (is.read(bytes) != -1) {
                int nCharCode = ((int) bytes[0]) & 0x00ff;

                if (nCharCode == 0) {
                    break;
                }
                if (nCharCode >= 0x80) {
                    if (nCharCode < 0xe0) {
                        // need 2 bytes
                        nCharCode = (nCharCode & 0x1f) << 6;
                        byte[] tmp = new byte[1];
                        is.read(tmp);
                        nCharCode |= (((int) tmp[0]) & 0x3f);
                    } else {
                        // need 3 bytes
                        nCharCode = (nCharCode & 0x0f) << 12;
                        byte[] tmp = new byte[2];
                        is.read(tmp);
                        nCharCode |= (((int) tmp[0]) & 0x3f) << 6;
                        nCharCode |= (((int) tmp[1]) & 0x3f);

                        // ignore character added by Notepad
                        if (nCharCode == 0xfeff) {
                            continue;
                        }
                    }
                }
                sb.append((char) nCharCode);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException ficheroNoExiste) {
            return "";
        }
        return sb.toString();
    }

    /**
     * 读取UTF编码的文件,形成字符串
     *
     * @param fn
     * @return
     */

    public final static String readUTFFileToString2(String fn) {
        StringBuffer buffer = null;
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            Class c = self.getClass();
            is = c.getResourceAsStream(fn);
            isr = new InputStreamReader(is, "UTF-8");
            buffer = new StringBuffer();
            int ch;
            while ((ch = isr.read()) > -1) {
                buffer.append((char) ch);
            }
            if (isr != null)
                isr.close();
        } catch (Exception ex) {

        }
        return buffer.toString();
    }

    /**
     * 去掉字串中的多行注释
     *
     * @param src
     * @return
     */

    public final static String delMutilLineComment(String src) {
        return deleteChars(src, "/*", "*/");
    }

    /**
     * 去掉字符串中用beginString和endString包围的部分。包括endString和beginString字串在内
     *
     * @param src
     *            待处理的源串
     * @param beginString
     * @param endString
     * @return
     */

    private final static String deleteChars(String src, String beginString,
                                            String endString) {
        if (src == null || src.length() == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();

        int firstIndex = src.indexOf(beginString, 0);
        if (firstIndex >= 0) {
            sb.append(src.substring(0, firstIndex));
        } else {

            return src;
        }

        int lastIndex = 0;
        while (firstIndex < src.length() && firstIndex >= 0) {
            lastIndex = src.indexOf(endString, firstIndex + 2);
            firstIndex = src.indexOf(beginString, lastIndex + 1);

            if (firstIndex > lastIndex) {
                String good = src.substring(lastIndex + 2, firstIndex);
                sb.append(good);
            }

        }

        if (firstIndex < 0 && lastIndex + 2 < src.length()) {
            sb.append(src.substring(lastIndex + 2));
        }

        return sb.toString();
    }

    /**
     * 一个较为快捷的碰撞检测方法
     *
     * @param ax
     *            a矩形左上角x坐标
     * @param ay
     *            a矩形左上角y坐标
     * @param aw
     *            a矩形宽度
     * @param ah
     *            a矩形高度
     * @param bx
     *            b矩形左上角x坐标
     * @param by
     *            b矩形左上角y坐标
     * @param bw
     *            b矩形宽度
     * @param bh
     *            b矩形高度
     * @return
     */
    public static final boolean isIntersectingRect(int ax, int ay, int aw,
            int ah, int bx, int by, int bw, int bh) {
        if (by + bh < ay || // is the bottom of b above the top of a?
            by > ay + ah || // is the top of b below bottom of a?
            bx + bw < ax || // is the right of b to the left of a?
            bx > ax + aw) // is the left of b to the right of a?
            return false;

        return true;
    }

    /**
     * 比较精灵S2相对于精灵S2的方位
     *
     * @param s1
     * @param s2
     * @return Canvas.DOWN:S1在S2的下方, Canvas.UP:S1在S2的上方, Canvas.LEFT:S1在S2的左方,
     *         Canvas.RIGHT:S1在S2的右方
     */
    public final static int comparePos(Sprite s1, Sprite s2) {
        int result = Canvas.DOWN;
        int ox1 = s1.getX() + s1.getWidth() / 2;
        int oy1 = s1.getY() + s1.getHeight() / 2;

        int ox2 = s2.getX() + s2.getWidth() / 2;
        int oy2 = s2.getY() + s2.getHeight() / 2;

        if (oy1 < oy2 && Math.abs(oy1 - oy2) > Math.abs(ox1 - ox2)) {
            return Canvas.UP;
        } else if (ox1 > ox2 && Math.abs(oy1 - oy2) < Math.abs(ox1 - ox2)) {
            return Canvas.RIGHT;
        } else if (ox1 < ox2 && Math.abs(oy1 - oy2) < Math.abs(ox1 - ox2)) {
            return Canvas.LEFT;
        }

        return result;
    }

    /**
     * 把Vector v2的所有元素都加到Vector v1中,相当于SE中Vector的addAll
     *
     * @param v1
     * @param v2
     * @return
     */
    public final static Vector addAll(Vector v1, Vector v2) {
        if (v1 != null && v2 != null) {
            for (int i = 0; i < v2.size(); i++) {
                v1.addElement(v2.elementAt(i));
            }
        }

        return v1;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?