import javax.microedition.lcdui.*; import java.io.UnsupportedEncodingException; import java.util.Random; import java.util.Vector; public class Tools { private static final int KEY_LS = -6; private static final int KEY_RS = -7; // public static DirectGraphics dg; public static Random randomizer = new Random(); //public static Image[] mmImg = new Image[6]; // 主菜单图片 public static int sceneID; public static int SCREEN_W; // 屏幕实际width public static int SCREEN_H; // 屏幕实际height // public static int screen_x; //屏幕的X // public static int screen_y; //屏幕的Y // public static int screen_w; //屏幕显示地图的width // public static int screen_h; //屏幕显示地图的height public static int loop; /*************************************************************************** * @return * @todo:公有函数 **************************************************************************/ public Tools() { } /** * 图片切帧函数 * * @param g Graphics * @param ix int 图片的X * @param iy int 图片的Y * @param w int 图片的W * @param h int 图片的H * @param px int 绘制图片的X * @param py int 绘制图片的Y * @param image Image */ public static void drawClip(Graphics g, int ix, int iy, int w, int h, int px, int py, Image image) { g.setClip(px, py, w, h); g.drawImage(image, px - ix, py - iy, 20); g.setClip(0, 0, SCREEN_W, SCREEN_H); } /** * 切图片方法 * @param g Graphics * @param x int * @param y int * @param w int * @param h int * @param frame int * @param image Image */ public static void drawClip(Graphics g, int x, int y, int w, int h, int frame, Image image) { int tempw = image.getWidth() / w; g.setClip(x, y, w, h); g.drawImage(image, x - (frame % tempw) * w, y - (frame / tempw) * h, 4 | 16); g.setClip(0, 0, SCREEN_W, SCREEN_H); } /** * 用图片的方式显示数字 * @param g Graphics * @param number int * @param x int * @param y int * @param w int * @param h int * @param image Image */ public static void drawNums(Graphics g, int number, int x, int y, int w, int h, Image image) { int len = String.valueOf(number).length(); String aa = String.valueOf(number); String[] NUM = new String[len]; for (int i = 0; i < NUM.length; i++) { NUM[i] = aa.substring(i, i + 1); drawClip(g, i * (w + 1) + x, y, w, h, Integer.parseInt(NUM[i]), image); } } /** * 将基本数据类型转换为byte数组,以及反向转换的方法 * 只涉及转换操作,对于参数没有进行校验 适用范围:RMS操作、网络数据传输 * 将int类型的数据转换为byte数组 * @param n int数据 * @return 生成的byte数组 */ public byte[] intToBytes(int n) { String s = String.valueOf(n); return s.getBytes(); } /** * 将byte数组转换为int数据 * @param b 字节数组 * @return 生成的int数据 */ public int bytesToInt(byte[] b) { String s = new String(b); return Integer.parseInt(s); } /** * 将int类型的数据转换为byte数组 原理:将int数据中的四个byte取出,分别存储 * @param n int数据 * @return 生成的byte数组 */ public byte[] intToBytes2(int n) { byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { b[i] = (byte) (n >> (24 - i * 8)); } return b; } /** * 将byte数组转换为int数据 * @param b 字节数组 * @return 生成的int数据 */ public int byteToInt2(byte[] b) { return (((int) b[0]) << 24) + (((int) b[1]) << 16) + (((int) b[2]) << 8) + b[3]; } /** * 获得一个nMin和 nMan 之间随机数 * @param nMin int * @param nMan int * @return 生成的int数据 */ public int Random(int nMin, int nMax) { return Math.abs(randomizer.nextInt()) % Math.abs(nMax + 1 - nMin) + nMin; } /** * 实现0~100度透明了//100表示完全不透明,0表示完全透明。可以做个渐变的效果 * @param srcImage Image 原图像 * @param alpha int * @return Image 返回一个Image对象 */ public Image alphaImage(Image srcImage, int alpha) { int imgW, imgH, length; imgW = srcImage.getWidth(); imgH = srcImage.getHeight(); length = imgW * imgH; int srcARGBimg[] = new int[length]; srcImage.getRGB(srcARGBimg, 0, imgW, 0, 0, imgW, imgH); int tmp = ((alpha * 255 / 100) << 24) | 0x00ffffff; for (int i = 0; i < srcARGBimg.length; i++) { srcARGBimg[i] &= tmp; } return Image.createRGBImage(srcARGBimg, imgW, imgH, true); } /** * 中文乱码的处理 * @param srcString String * @return String 返回一个String对象 */ public String getChineseString(String srcString) { byte[] b = new byte[srcString.length()]; try { b = srcString.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } return (new String(b)); } /** * 画出分割好的字符串组 * @param clipStr—先前分割好的字符串数组 * @param font—字体 * @param color—颜色 * @param x,y—打印的屏幕位置 */ public static final void drawClipString(Graphics g, String[] clipStr, Font font, int color, int x, int y) { if (clipStr == null) { System.out.println("drawClipString"); return; } int fontH = font.getHeight(); g.setFont(font); g.setColor(color); for (int i = 0; i < clipStr.length; i++) g.drawString(clipStr[i], x, y + i * fontH, 0); } /** * 分割字符串 * @param str—要处理的字符串 * @param font—字体 * @param rowMaxW—分割后每行宽度[象素直] *其中/n是换行 */t是空格 */ public static final String[] clipString(String str, Font font, int rowMaxW) { if (str == null) return null; if (rowMaxW < font.charWidth('田')) rowMaxW = font.charWidth('田'); int strID = 0; int rowW = 0; Vector strManager = new Vector(); char ch = ' '; StringBuffer sb = new StringBuffer(str); while (str.length() > strID) { ch = str.charAt(strID); switch (ch) { case '/n': strManager.addElement(str.substring(0, strID)); str = str.substring(strID + 1); rowW = 0; strID = 0; break; case '/t': sb.delete(0, sb.length()); sb.append(str); sb.deleteCharAt(strID); sb.insert(strID, " "); str = sb.toString(); break; default: if (rowW + font.charWidth(ch) > rowMaxW) { strManager.addElement(str.substring(0, strID)); str = str.substring(strID); rowW = 0; strID = 0; } else { rowW += font.charWidth(ch); strID++; } } } strManager.addElement(str); String[] o_Str = new String[strManager.size()]; strManager.copyInto(o_Str); return o_Str; } }