⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tool.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;

public class Tool {
	public static Tool getInstance(){
		if(tool == null){
			tool = new Tool();
		}

		return tool;
	}
	
	public static void fillScreen(Graphics g, int color) {
		g.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
						GameConstant.iSCREEN_HEIGHT);
		
		g.setColor(color);
		
		g.fillRect(0, 0, GameConstant.iSCREEN_WIDTH,
				GameConstant.iSCREEN_HEIGHT);
	}
    
    public static int sin(int angle){
    	for (; angle < 0; angle += 360);
		return iSinTab[angle % 360];
    }
    
    public static int cos(int angle){
    	return sin(angle + 90);
    }
    
    //  所有正弦函数值域表
	private static int iSinTab[] = {
        0, 4, 8, 13, 17, 22, 26, 31, 35, 40,
        44, 48, 53, 57, 61, 66, 70, 74, 79, 83, 
        87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 
        128, 131, 135, 139, 143, 146, 150, 154, 157, 161, 
        164, 167, 171, 174, 177, 181, 184, 187, 190, 193, 
        196, 198, 201, 204, 207, 209, 212, 214, 217, 219, 
        221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 
        240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 
        252, 252, 253, 254, 254, 255, 255, 255, 255, 255, 
        256, 255, 255, 255, 255, 255, 254, 254, 253, 252,
        252, 251, 250, 249, 248, 247, 246, 244, 243, 242,
        240, 238, 237, 235, 233, 232, 230, 228, 226, 223,
        221, 219, 217, 214, 212, 209, 207, 204, 201, 198,
        196, 193, 190, 187, 184, 181, 177, 174, 171, 167,
        164, 161, 157, 154, 150, 146, 143, 139, 135, 131,
        128, 124, 120, 116, 112, 108, 104, 100, 95, 91,
        87, 83, 79, 74, 70, 66, 61, 57, 53, 48,
        44, 40, 35, 31, 26, 22, 17, 13, 8, 4,
        -1, -5, -9, -14, -18, -23, -27, -32, -36, -41, 
        -45, -49, -54, -58, -62, -67, -71, -75, -80, -84, 
        -88, -92, -96, -101, -105, -109, -113, -117, -121, -125, 
        -128, -132, -136, -140, -144, -147, -151, -155, -158, -162, 
        -165, -168, -172, -175, -178, -182, -185, -188, -191, -194, 
        -197, -199, -202, -205, -208, -210, -213, -215, -218, -220, 
        -222, -224, -227, -229, -231, -233, -234, -236, -238, -239, 
        -241, -243, -244, -245, -247, -248, -249, -250, -251, -252, 
        -253, -253, -254, -255, -255, -256, -256, -256, -256, -256, 
        -256, -256, -256, -256, -256, -256, -255, -255, -254, -253, 
        -253, -252, -251, -250, -249, -248, -247, -245, -244, -243, 
        -241, -239, -238, -236, -234, -233, -231, -229, -227, -224, 
        -222, -220, -218, -215, -213, -210, -208, -205, -202, -199, 
        -197, -194, -191, -188, -185, -182, -178, -175, -172, -168, 
        -165, -162, -158, -155, -151, -147, -144, -140, -136, -132, 
        -129, -125, -121, -117, -113, -109, -105, -101, -96, -92, 
        -88, -84, -80, -75, -71, -67, -62, -58, -54, -49, 
        -45, -41, -36, -32, -27, -23, -18, -14, -9, -5
    };
	
	public static int getCenterX(int xPos,int angle,int radius){
		return xPos - radius*cos(angle);
	}
	
	public static int getCenterY(int yPos,int angle,int radius){
		return yPos + radius*sin(angle);
	}
	
	public static int getXSpeedByAngel(int angel,int speed){
		return cos(angel) * speed;
	}
	
	public static int getYSPeedByAngel(int angel,int speed){
		return sin(angel) * speed;
	}
	
	public static int sqrt(int srcNumber) {
		if (srcNumber > 0) {
			int j;
			if (srcNumber > 100)
				j = srcNumber;
			else
				j = 100;
			int k;
			do {
				k = j;
				j = srcNumber / j + j >> 1;
			} while (j < k);
			return k;
		} else {
			return 0;
		}
	}
	
	
	/*
	 * 获得从一个每隔多少角度的X方向的速度
	 * @ angleStart 起始角
	 * @ angleNum   产生多少个角度
	 * @ angle      角度
	 * @ speed      运动的速度
	 */
	public static int[] getXSpeedByAngle(int angleStart, int angleNum,
			int angle, int speed) {
		xSpeed = new int[angleNum];
		for (int idx = 0; idx < angleNum; idx++) {
			xSpeed[idx] = getXSpeedByAngel(angleStart - angle * idx, speed);
		}

		return xSpeed;
	}
	
	public static int[] getYSpeedByAngle(int angleStart, int angleNum,
			int angle, int speed) {
		ySpeed = new int[angleNum];
		for (int idx = 0; idx < angleNum; idx++) {
			ySpeed[idx] = getYSPeedByAngel(angleStart - angle * idx, speed);
		}

		return ySpeed;
	}
	
	public static int abs(int srcNum)
    {
        return srcNum >= 0 ? srcNum : ~srcNum + 1;
    }
	
	//获得2点的距离
	public static int getDistance(int x,int y,int x1,int y1){
		return (x1 - x) * (x1 - x) + (y1 - y) * (y1 - y);
	}

	/**
	 * 检测2个矩形是否向碰撞
	 * @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 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 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;
	}
	

//	public static Vector getSubsection(String strSource,Font font,int width,String strSplit)
//    {
//         vector = new Vector();
//         String temp=strSource;
//         int i,j;
//         int LastLength = 1;
//         int step = 0;
//         try{
//             while (!temp.equals("")) {
//               i=temp.indexOf("\n");
//               if(i>0){
//                 if(font.stringWidth(temp.substring(0,i-1)) >= width){
//                   i = -1;
//                 }
//               }
//               if(i==-1){
//                 if(LastLength>temp.length()){
//                   i = temp.length();
//                 }else{
//                   i = LastLength;
//                   step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
//                   if(i<temp.length()){
//                     while (! (font.stringWidth(temp.substring(0, i)) <= width
//                               && font.stringWidth(temp.substring(0, i + 1)) > width)) {
//                       i = i + step;
//                       if (i == temp.length())
//                         break;
//                     }
//                   }
//                 }
//                 if(!strSplit.equals("")){
//                   j = i;
//                   if (i < temp.length()) {
//                     while (strSplit.indexOf(temp.substring(i-1,i))==-1) {
//                       i--;
//                       if (i == 0) {
//                         i = j;
//                         break;
//                       }
//                     }
//                   }
//                 }
//               }
//               LastLength = i;
//               vector.addElement(temp.substring(0, i));
//               if (i == temp.length()) {
//                 temp = "";
//               }
//               else{
//                 temp = temp.substring(i);
//                 if (temp.substring(0, 1).equals("\n")) {
//                   temp = temp.substring(1);
//                 }
//               }
//             }
//         }catch(Exception e)
//         {
//           //System.out.println("getSubsection:"+e);
//         }
//
//         temp = null;
//         return vector;
//    }
//	
//	public static void releaseVector(){
//		vector.removeAllElements();
//		vector = null;
//	}
	
	//@i 底是10的几次方
	public static int square(int i) {
		int sum = 1;
		for (int idx = 0; idx < i; idx++) {
			sum = sum * 10;
		}
		return sum;
	}

	private static int [] xSpeed;
	private static int [] ySpeed;
	//private CImage [] numImage = null;
	private static Tool tool;
	
	//private static Vector vector;
}

⌨️ 快捷键说明

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