📄 tools.java
字号:
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 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -