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


package demo; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.game.GameCanvas; 
import javax.microedition.lcdui.game.Sprite; 
 
 
public class ButtonSprite extends Sprite{ 
	//精灵状态值 
	public static final int BUTTON_WALK 	= 0;			//WALK按钮 
	public static final int BUTTON_TALK	= 1;			//TALK按钮 
	public static final int BUTTON_SLEEP = 2;				//SLEEP按钮 
	public static final int BUTTON_MUSIC	= 3;			//MUSIC按钮 
	public static final int BUTTON_TYPE_NUM	= 4; 
	private int m_nCurButton = BUTTON_WALK; 
	public ButtonSprite(Image img, int frameWidth, int frameHeight){ 
		super(img, frameWidth, frameHeight ); 
		defineReferencePixel(frameWidth/2, frameHeight/2); 
		setVisible(false); 
	} 
	//响应按键,返回所选按钮的种类 
	public int Input(int keyStates){ 
		if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ) 
		{ 
			if( isVisible() ) 
			{//按下按钮 
				setVisible(false); 
				return m_nCurButton; 
			} 
			else 
			{//调出按钮 
				setVisible(true); 
				return -1; 
			} 
		} 
		if( isVisible() ) 
		{//选择按钮 
			if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ) 
				m_nCurButton --; 
			if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ) 
				m_nCurButton ++; 
			if( m_nCurButton < 0 ) 
				m_nCurButton = 0; 
			else if( m_nCurButton >= BUTTON_TYPE_NUM ) 
				m_nCurButton = BUTTON_TYPE_NUM - 1; 
		} 
		return -1; 
	} 
	//显示所有按钮 
	public void PaintAll(Graphics g, int scrWidth, int scrHeight){ 
		if( isVisible() == false ) 
			return; 
		int x = scrWidth/2 - getWidth()/2; 
		int y = scrHeight/2 -getHeight() * 2 - 20; 
		for( int n = 0; n < BUTTON_TYPE_NUM; n ++ ){ 
			setPosition( x, y); 
			setFrame(n); 
			paint(g); 
			if( n == m_nCurButton ){ 
				g.drawRect(x-1, y-1,  
					getWidth()+2, getHeight()+2); 
			} 
			y = y + getHeight() + 10; 
		}	 
	} 
}