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

📄 util.java

📁 J2ME程序开发实用例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.DataInputStream;
import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.*;

//import com.nokia.mid.ui.DirectGraphics;
/**
 * <p>
 * Title: abstract GameUtil Class
 * </p>
 * <p>
 * Description: 本游戏适用于MIDP1.0版本的手机
 * </p>
 * <p>
 * Copyright: 本游戏由<<QN|刹那>>制作,不可用于商业.QQ173214905
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author QN
 * @version 2.0
 */
public class Util extends Canvas implements Runnable {
	public Util(){
		setFullScreenMode(true) ;
		loadScreen(0,0);
	}
//	public abstract void draw(Graphics g);
//	public abstract void ini(byte i);
	public static final int SCREEN_W = 176,SCREEN_H = 208;
	/** ******************************************************Nokia*SE K700 TODO:<按键定义> */
	public static final byte KEY_UP = 1;
	public static final byte KEY_DOWN = 2;
	public static final byte KEY_LEFT = 3;
	public static final byte KEY_RIGHT = 4;
	public static final byte KEY_SOFT_RETURN = 7;
	public static final byte KEY_SOFT_FIRE = 6;
	public static final byte KEY_FIRE = 5;
//	public static DirectGraphics dg;// 非NOKIA机型,此将被删除
	public static final boolean isDebug = false;
	public static final int TheradFps = 65 ;// 刷新率
	/** 设定字体 */
	public static Font font =Font.getFont(0,0,0);
	public static final int FONT_W = font.charWidth('中') <= 0 ? font.stringWidth("qn") : font.charWidth('中'); // 中文字体宽
	public static final int FONT_H = font.getHeight() <= 0 ? FONT_W + 2 : font.getHeight();
	
	/** ***************************************************** */
	public int poiVaule;
	public int keyCode;
	public boolean disableKey;
	public boolean disablePlayerKey; 
	public int douKeyCode;// 双击按键值
	public int pressedKeyCode;
	
	public void key_clear(){
		keyCode = 0;
		douKeyCode = 0;
		pressedKeyCode = 0;
	}
	
	/**
	 * 使用双线程加载资源
	 */
	public void setGameState(byte iniState,boolean isHide){
//		new Loading(iniState, iniState,isHide);
		if(gameState != gameState_old)gameState_old = iniState;
//		wobble_reset();
	}
	
	public void setGameState(byte newState){
		if(gameState != gameState_old)
			gameState_old = gameState;
//		ini(newState);
		gameState = newState;
//		wobble_reset();
	}
	public static byte gameState ;
	public static byte gameState_old = gameState;
	public static byte tempPauseGameState = gameState ;	
	public static final byte GAME_LOGO = 0;
	public static final void println(String s){
		
	}
	public static final void Error(Exception e,String s){
		
	}
	/***************************************************************************
	 * TODO:本游戏
	 * 
	 * 
	 * 
	 */
	
	
	// 主角
	public Sprite player;
	static StringBuffer errorString = new StringBuffer();
	public void keyPre(int key0, int maxSize, int offset) {
		switch (key0) {
			case KEY_NUM2:
			case KEY_UP:
				poiVaule = select(poiVaule, maxSize, -offset);
				break;
			case KEY_NUM8:
			case KEY_DOWN:
				poiVaule = select(poiVaule, maxSize, offset);
				break;
			case KEY_NUM4:
			case KEY_LEFT:
				break;
			case KEY_NUM6:
			case KEY_RIGHT:
				break;
		}
	}
	public static int select(int poiVaule,int max,int offset) {
		poiVaule+=offset ;
		if (offset < 0) {
			if (poiVaule < 0)
				poiVaule = max ;
		}else{
			if (poiVaule > max)
				poiVaule = 0 ;
		}
		
		return poiVaule;
	}
	
	//
	public void paint(Graphics g) {
		{
//			wobble_draw(g);//进行画面控制(抖动)
//			dg = com.nokia.mid.ui.DirectUtils.getDirectGraphics(g);
			draw(g);
		}
		pressedKeyCode = 0;
	}
	
	public void run() {
		while (true) {
			try {
				long startTime = System.currentTimeMillis();
				repaint();
				serviceRepaints();
				Thread.yield();
				// System.gc();
				long endTime = System.currentTimeMillis();
				if (endTime - startTime < TheradFps) {
					Thread.sleep(TheradFps - (endTime - startTime ));
				}
			} catch (Exception e) {
				Error(e,"");
				Util.println("maniRun");
			}
		}
	}
	
	
	/** ************************************************************TODO:<绘制图片支持翻转,支持描点> */
	public static void drawImage(Graphics g, Image image, int x, int y, int flipFlag, int anc) {
		drawRegion(g,image,0,0,image.getWidth(),image.getHeight(),flipFlag,x,y,anc);
	}
	/** ******************************************************************TODO:<从大图绘制指定平分的区域ID> */
	public void drawFrame(int ID, Image image, int x, int y,int flip, int anchor, Graphics g, int clipW,
			int clipH) {
		if (ID == - 1) {
			return;
		}
		int imgCol = image.getWidth() / clipW;//
		int clipX = (ID % imgCol ) * clipW;
		int cilpY = (ID / imgCol ) * clipH;
		drawRegion(g,image,clipX,cilpY,clipW,clipH,flip,x,y,anchor);
	}
	
	/** *******************************************************************TODO:<切图绘制> */
	public static void drawRegion(Graphics g, Image image, int clipX, int clipY, int clipW,
			int clipH, int flipFlag, int x, int y, int anchor) {
		if (image == null) return;
		int[] offsetXY = getAnchorOffset(anchor,clipW,clipH);
		g.setClip(x + offsetXY[0],y + offsetXY[1],clipW,clipH);
		g.drawImage(image,x - clipX + offsetXY[0],y - clipY + offsetXY[1],0);
		g.setClip(0,0,SCREEN_W + 20,SCREEN_H + 20);
	}
	public static int[] getAnchorOffset(int anchor, int w, int h) {
		int offX = 0;
		int offY = 0;
		switch (anchor) {
			case Graphics.HCENTER | Graphics.TOP:
				offX = w >> 1;
			break;
			case Graphics.RIGHT | Graphics.TOP:
				offX = w;
			break;
			case Graphics.LEFT | Graphics.VCENTER:
				offY = h >> 1;
			break;
			case Graphics.HCENTER | Graphics.VCENTER:
				offX = w >> 1;
				offY = h >> 1;
			break;
		}
		return new int[] { - offX, - offY };
	}// :~QN

	/** *******************************************************************TODO:<切图绘制Nokia> */	
	/** ******************************************************************TODO:<绘制通用文本> */
	/** ******************************************************************TODO:<绘制描边文字> */
	/** ******************************************************************TODO:<中英文自然分行分页> */
	/** ******************************************************************TODO:<绘制左右下角标记> */
	/** ******************************************************************TODO:<产生随机数> */
	/** ******************************************************************TODO:<自定义按键> */
	/** ******************************************************************TODO:<保存用户按键设置> */
	/** ******************************************************************TODO:<加载用户按键设置> */
	/** ******************************************************************TODO:<保存数据类> */	
	/** ******************************************************************TODO:<按键双击> */	
	
//	绘制指定区域,双缓冲提升速度.
	 Image MAP_BUFIMG ;
	 Graphics MAP_BUFG  ;	
	public void map_drawBg(Graphics g,int colBen, int rowBen, int colEnd, int rowEnd) {
		int count = 0;
		int j, i;
		MAP_BUFG.setColor(0);
		MAP_BUFG.fillRect(0,0,SCREEN_W,SCREEN_H);
		for (i = rowBen; i <= rowEnd && i < map_data.length; i ++) {
			for (j = colBen; j <= colEnd && j < map_data[0].length; j ++) {
				int idShort = map_data[i][j];
				int x = (j % ((SCREEN_W / map_tileW ) + 1) ) << 3;
				int y = (i % ((SCREEN_H / map_tileH ) + 1) ) << 3;
				int id = (byte)(idShort & 0xff );
				int flipFlag = (byte)((idShort >> 12 ) & 0xff );
				int clipX = (id % map_imgCol ) << 3;
				int clipY = (id / map_imgCol ) << 3;
				drawRegion(MAP_BUFG,res_getImage(RES_TILE),clipX,clipY,map_tileW,map_tileH,0,x,y,0);
			}
		}
		g.drawImage(MAP_BUFIMG,0,0,Graphics.LEFT | Graphics.TOP);
	}
	
	
	
	
	// map
	public short[][] map_data;
//	public byte[][] map_physicsData;
	int map_tileW,
	map_tileH,
	map_imgCol,
	mapX,mapY,
	mapW,mapH;	
	
	/***************************************************************************
	 * //TODO:精灵管理
	 **************************************************************************/
	private Vector spriteS = new Vector();// 精灵集合
//	private Arraylist spriteS = new Arraylist();// 精灵集合
	private int sprListID = 0;// 集合里的列表ID 永远固定
	// -PLAY
	public static final byte ID_PLAYER = 0;// 主角
	public static final byte ID_BULLET = 1;// 子弹
	public static final byte ID_BULLET_BOOM = 2;
	public static final int ID_BRICK = 3;
	public static final int ID_ROCK = 4;
	public static final int ID_WATER = 5;
	public static final int ID_ICE = 6;
	public static final int ID_BLOCKHOUSE = 7;
	
	
	public void sprite_changeSpeed(Sprite spt,int xSpeed,int ySpeed){
		spt.vx =  xSpeed;
		spt.vy =  ySpeed;
	}
	
	public void sprite_changePosition(Sprite spt) {
		spt.x += spt.vx;
		spt.y += spt.vy;
		
	}
	
	public void sprite_add(int id,int dir,String onlyName){//
		Sprite spt = new Sprite();
		spt.id = (byte) id;
		switch(id){// 角色配制信息
			case ID_PLAYER:
				spt.w = 14;
				spt.h = 14;
				spt.resList = new int[]{RES_TK};
				break;
			case ID_BULLET:
				spt.w = 4;
				spt.h = 4;
				spt.resList = new int[]{RES_BULLET};
				break;
			case ID_BULLET_BOOM:
				spt.resList = new int[]{RES_BULLET_BOOM};
				spt.w = 14;
				spt.h = 14;
				break;
			default:
				spt.w = map_tileW;
			spt.h = map_tileH;				
			break;
		}
		res_load(spt.resList);
		
		sprite_setDir(spt,dir);
		
		spriteS.addElement(spt);
		sprListID++;
	}
	
	
	public void sprite_setDir(Sprite spt,int dir){
		spt.setDir(dir);
	}
	
	
	
	public int sprite_getSize(){
		return spriteS.size();
	}
	
	public Sprite sprite_getOnIndex(int index){
		return (Sprite)spriteS.elementAt(index);
	}
	
	public int sprite_getIndex(int listId)
	{
		Sprite spt = null;
		for(int i = 0; i < sprite_getSize(); i++)
		{
			spt = (Sprite)spriteS.elementAt(i);
			if(spt.listID == listId)
				return i;
		}
		
		return -1;
	}
	
	
	
	
	/** ******************************************************************TODO:<图片工具> */
	public static Image createImage(String fileName) {
		try {
			Util.println(fileName + " open");
			return Image.createImage("/res/" + fileName);
		} catch (Exception e) {
			Error(e, "fileName " + fileName);
			return null;
		}
	}
	
	
	/** ******************************************************************TODO:<抖动特效> */
	/** ******************************************************************TODO:<拉幕特效> */
	/** ******************************************************TODO:加载游戏场景 */
	
	
	// loadString
	// loadScreen
	// loadSprite
	// sound_loadAll()
	public boolean newGame;
	public void releaseAll(){
		res_releaseAll();// 图形资源
		map_data = null;
		MAP_BUFG = null;
		MAP_BUFIMG = null;
		key_clear();
		System.gc();
	}
	
	public void loadScreen(int tileID,int mapDataID){
		releaseAll();
		if(player == null){
			sprite_add(ID_PLAYER,Sprite.DIR_UP,"p");
			player = sprite_getOnIndex(0);
			MAP_BUFIMG = Image.createImage(SCREEN_W,SCREEN_H);
			MAP_BUFG = MAP_BUFIMG.getGraphics();	
		}
		try {
//			String fileName = resNames[resID];
			DataInputStream dis = openStream("/res/m"+mapDataID+".bin");
			dis.readUnsignedByte();

⌨️ 快捷键说明

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