📄 mybutton.java
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
public class MyButton {
//定义一组表示按钮种类的数值
public static final int BUTTON_START = 0; //开始按钮
public static final int BUTTON_RESET = 1; //复位按钮
public static final int BUTTON_CONTINUE = 2; //继续按钮
public static final int BUTTON_HELP = 3; //帮助按钮
public static final int BUTTON_SAVE = 4; //保存按钮
public static final int BUTTON_EXIT = 5; //退出按钮
public static final int BUTTON_TYPE_NUM = 6; //按钮种类
private Sprite m_ButtonSp; //按钮精灵图像
private int m_aIndex[]; //按钮的种类
private boolean m_abShow[]; //按钮是否显示的标志
private int m_nShowNum = 0; //当前显示的按钮数量
private int m_nFocusType = -1; //高亮按钮类型
//构造方法
//参数width,height设置按钮精灵的宽和高
//6个Index参数分别是各类型的按钮在精灵图像中的索引,-1表示没有这种按钮的图像
public MyButton(int width, int height, int startIndex, int resetIndex,
int continueIndex, int helpIndex, int saveIndex, int exitIndex){
m_aIndex = new int[BUTTON_TYPE_NUM];
m_abShow = new boolean[BUTTON_TYPE_NUM];
m_nShowNum = 0;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ ){
m_abShow[n] = false;
m_aIndex[n] = -1;
}
try{
Image img = Image.createImage("/demo/button.png");
m_ButtonSp = new Sprite(img, width, height);
m_ButtonSp.defineReferencePixel(width/2, height/2);
}
catch (Exception ex){}
m_aIndex[BUTTON_START] = startIndex;
m_aIndex[BUTTON_RESET] = resetIndex;
m_aIndex[BUTTON_CONTINUE] = continueIndex;
m_aIndex[BUTTON_HELP] = helpIndex;
m_aIndex[BUTTON_SAVE] = saveIndex;
m_aIndex[BUTTON_EXIT] = exitIndex;
}
//设置各种按钮的显示标志
public void setShow( boolean bStart, boolean bReset, boolean bContinue,
boolean bHelp, boolean bSave, boolean bExit ){
m_abShow[BUTTON_START] = bStart;
m_abShow[BUTTON_RESET] = bReset;
m_abShow[BUTTON_CONTINUE] = bContinue;
m_abShow[BUTTON_HELP] = bHelp;
m_abShow[BUTTON_SAVE] = bSave;
m_abShow[BUTTON_EXIT] = bExit;
m_nShowNum = 0;
m_nFocusType = -1;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ ){
if( m_abShow[n] && m_aIndex[n] >= 0 ){
//设置第一个显示的按钮为当前高亮的按钮
if( m_nFocusType < 0 )
m_nFocusType = n;
m_nShowNum++;
}
}
}
//处理按键的输入,返回选择的按键类型
public int Input(int keyStates){
if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ){
int temp = m_nFocusType - 1;
while( temp >= 0 ){
if( m_abShow[temp] && m_aIndex[temp] >= 0 ){
m_nFocusType = temp;
break;
}
temp --;
}
}
if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ){
int temp = m_nFocusType + 1;
while( temp < BUTTON_TYPE_NUM ){
if( m_abShow[temp] && m_aIndex[temp] >= 0 ){
m_nFocusType = temp;
break;
}
temp ++;
}
}
if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ){
return m_nFocusType;
}
return -1;
}
//显示按钮
public void Paint(Graphics g, int scrWidth, int scrHeight){
if( m_ButtonSp == null )
return;
int h = m_ButtonSp.getHeight() + 2;
int x = 15;
int y = scrHeight - h * m_nShowNum - 15;
for( int n = 0; n < BUTTON_TYPE_NUM; n ++ )
{
if( m_abShow[n] && m_aIndex[n] >= 0 )
{//如果需要显示按钮,且该类型按钮的图像存在,则显示
if( m_nFocusType == n )
{//显示高亮的按钮
m_ButtonSp.setFrame(m_aIndex[n]);
m_ButtonSp.setPosition(x, y);
m_ButtonSp.paint(g);
}
else
{//显示灰色按钮
int half = m_ButtonSp.getFrameSequenceLength() / 2;
m_ButtonSp.setFrame(m_aIndex[n] + half);
m_ButtonSp.setPosition(x, y);
m_ButtonSp.paint(g);
}
y = y + h;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -