www.pudn.com > 50code.rar > MainCanvas.java, change:2007-02-26,size:3261b


package demo; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.game.GameCanvas; 
 
 
public class MainCanvas extends GameCanvas implements Runnable{ 
	public static final int GAME_UI		= 0;	//进入用户界面 
	public static final int GAME_GAMING	= 1;	//进行游戏 
	public static final int GAME_END		= 2;	//游戏结束 
	public static final int GAME_STATE_NUM = 3;	//状态总数 
	private int m_nState	= GAME_UI;				//存储当前的游戏状态 
	 
	private boolean	m_bRunning;					//控制线程运行 
	private MyUI 		m_UI;						//界面对象 
	private MyBack		m_Back;						//背景对象 
	private BKQSprite 	m_BKQ;						//宠物对象 
	private ButtonSprite m_Button;					//按钮对象 
	public MainCanvas(){ 
		super(true); 
		m_UI 	= new MyUI(); 
		m_Back	= new MyBack(); 
		try{ 
			//创建宠物 
			Image img = Image.createImage("/demo/BKQ.png"); 
			m_BKQ = new BKQSprite(img,54,59);	 
			m_BKQ.setAllPosition(getWidth()/2 - 30, getHeight() - 70 ); 
			//创建按钮 
			Image imgBT = Image.createImage("/demo/button.png"); 
			m_Button = new ButtonSprite(imgBT, 46, 16); 
		} 
		catch (Exception ex){} 
		Start();									//启动线程 
	} 
	public void Start(){ 
		m_bRunning = true; 
		Thread thread = new Thread(this);			//分配新线程 
		thread.start();								//线程启动 
	} 
	public void run() {							//新线程自动调用此方法 
		//获取系统当前时间,并将时间换算成以毫秒为单位的数 
		long T1 = System.currentTimeMillis(); 
		long T2 = T1; 
		while(m_bRunning){ 
			T2 = System.currentTimeMillis(); 
			if( T2 - T1 > 200 ){			    	//间隔200毫秒 
				T1 = T2; 
				Input(); 
				Logic(); 
				Paint(); 
			} 
		} 
	} 
	public void Stop(){							//终止游戏 
		m_bRunning = false; 
	} 
	public void Input(){ 
		int keyStates = getKeyStates(); 
		switch( m_nState ){ 
		case GAME_UI: 
			m_UI.Input( keyStates ); 
			if( m_UI.getState() == MyUI.UI_HIDE ) 
				m_nState = GAME_GAMING; 
			break;			 
		case GAME_GAMING: 
			switch( m_Button.Input(keyStates) ) 
			{//根据所按的按钮,设置精灵的状态 
			case ButtonSprite.BUTTON_WALK: 
				m_BKQ.setState(BKQSprite.BKQ_WALK); 
				break; 
			case ButtonSprite.BUTTON_TALK: 
				m_BKQ.setState(BKQSprite.BKQ_TALK); 
				break; 
			case ButtonSprite.BUTTON_SLEEP: 
				m_BKQ.setState(BKQSprite.BKQ_SLEEP); 
				break; 
			case ButtonSprite.BUTTON_MUSIC: 
				m_BKQ.setState(BKQSprite.BKQ_MUSIC); 
				break; 
			} 
			break; 
		} 
	} 
	public void Logic(){ 
		switch( m_nState ){ 
		case GAME_UI:								//界面逻辑 
			break;			 
		case GAME_GAMING:							//精灵逻辑 
			m_BKQ.Logic(); 
			if( m_BKQ.getState() == BKQSprite.BKQ_DEAD ) 
				m_nState = GAME_END; 
		} 
	} 
	public void Paint(){ 
		Graphics g = getGraphics(); 
		//用黑色清屏 
		g.setColor(0); 
		g.fillRect( 0, 0, getWidth(), getHeight() ); 
		 
		g.setColor(0x0000FF); 
		switch( m_nState ){ 
		case GAME_UI:								//显示界面 
			m_UI.Paint(g, getWidth(), getHeight()); 
			break;			 
		case GAME_GAMING:							//显示游戏画面 
			m_Back.Paint(g, getWidth(), getHeight()); 
			m_BKQ.PaintAll(g); 
			m_Button.PaintAll(g, getWidth(), getHeight()); 
			break; 
		case GAME_END:								//显示结束语 
			g.drawString("小精灵死了!!!!",  
					getWidth()/2 - 10, getHeight()/2 - 10, 0); 
			break; 
		} 
		flushGraphics(); 
	} 
}