?? tools.java
字號:
package com.thinkenjoy.tools;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* 工具類
*/
public final class Tools {
public static final int ARROW_LEFT = 0;
public static final int ARROW_RIGHT = 1;
public static final int ARROW_UP = 2;
public static final int ARROW_DOWN = 3;
private static final Random randomizer = new Random();
/**
* 獲得相應范圍內的隨機數
*
* @param min
* 取值范圍內的最小值
* @param max
* 取值范圍內的最大值
* @return
*/
public static final int getRand(int min, int max) {
int r = Math.abs(randomizer.nextInt());
return (r % ((max - min + 1))) + min;
}
/**
* 經常要用到的設置變量
*/
public static final int GRAPHICS_TOP_LEFT = Graphics.LEFT | Graphics.TOP;
/**
* 按相應的長度擴大一個數組
*
* @param 舊的需要擴充的數組
* @param 需要擴充的長度
* @return 擴充后的新數組 (復制舊數組并增大相應的長度
*/
public final static int[] expandArray(int[] oldArray, int expandBy) {
int[] newArray = new int[oldArray.length + expandBy];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
/**
* 按相應的長度擴大一個二維圖片數組
* @param 舊的需要擴充的數組
* @param 需要擴充的長度
* @return 擴充后的新數組 (復制舊數組并增大相應的長度
*/
public final static Image[][] expandArray(Image[][] oldArray, int expandBy) {
Image[][] newArray = new Image[oldArray.length + expandBy][];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
/**
* 根據圖片名讀取圖片
* @param fileName
* 圖片名
* @return 讀取的圖片
*/
public static Image getImage(String fileName) {
try {
return Image.createImage("/" + fileName + ".png");
} catch (Exception e) {
System.out.println("loading image error! " + fileName + ".png");
}
return null;
}
/**
* 畫藝術字
*
* @param g
* @param mainColor
* @param topColor
* @param bottomColor
* @param str
* @param x
* @param y
* @param anchor
*/
public static void drawArtString(Graphics g, int mainColor, int topColor,
int bottomColor, String str, int x, int y, int anchor) {
if (topColor >= 0) {
g.setColor(topColor);
g.drawString(str, x - 1, y, anchor);
g.drawString(str, x, y - 1, anchor);
g.drawString(str, x - 1, y - 1, anchor);
}
if (bottomColor >= 0) {
g.setColor(bottomColor);
g.drawString(str, x + 1, y, anchor);
g.drawString(str, x, y + 1, anchor);
g.drawString(str, x + 1, y + 1, anchor);
}
g.setColor(mainColor);
g.drawString(str, x, y, anchor);
}
/**
* 將原始的信息逐行分解并存入相應容器
*
* @param sb
* Vector容器
* @param originalInfo
* 原始的字符串
* @param textWidth
* 一行的寬度
*/
public static void divideString(Vector sb, String originalInfo, int textWidth) {
int offSet = 0;
for (int i = 0; i < originalInfo.length(); i++) {
if (i == originalInfo.length() - 1) {
if (Font.getDefaultFont().substringWidth(originalInfo, offSet,
i + 1 - offSet) >= textWidth) {
sb.addElement(originalInfo.substring(offSet, i));
sb.addElement(originalInfo.substring(i, i + 1));
} else {
sb.addElement(originalInfo.substring(offSet, i + 1));
}
} else if (Font.getDefaultFont().substringWidth(originalInfo,
offSet, i + 1 - offSet) >= textWidth) {
sb.addElement(originalInfo.substring(offSet, i));
offSet = i;
}
}
}
/**
* 繪制各個方向的箭頭
* @param g
* @param pos1x
* @param pos1y
* @param direct
*
*/
public static void drawArrow(Graphics g, int pos1x, int pos1y, int direct) {
switch (direct) {
case ARROW_LEFT:
g.drawLine(pos1x, pos1y, pos1x - 9, pos1y);
g.drawLine(pos1x - 5, pos1y + 3, pos1x - 9, pos1y);
g.drawLine(pos1x - 5, pos1y - 3, pos1x - 9, pos1y);
break;
case ARROW_RIGHT:
g.drawLine(pos1x, pos1y, pos1x + 9, pos1y);
g.drawLine(pos1x + 5, pos1y + 3, pos1x + 9, pos1y);
g.drawLine(pos1x + 5, pos1y - 3, pos1x + 9, pos1y);
break;
case ARROW_UP:
g.drawLine(pos1x, pos1y, pos1x, pos1y - 9);
g.drawLine(pos1x + 3, pos1y - 5, pos1x, pos1y - 9);
g.drawLine(pos1x - 3, pos1y - 5, pos1x, pos1y - 9);
break;
case ARROW_DOWN:
g.drawLine(pos1x, pos1y, pos1x, pos1y + 9);
g.drawLine(pos1x + 3, pos1y + 5, pos1x, pos1y + 9);
g.drawLine(pos1x - 3, pos1y + 5, pos1x, pos1y + 9);
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -