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