📄 gamedialogbox.java
字号:
package cn.org.matrix.gmatrix.gameLab.util.ui;
import javax.microedition.lcdui.Graphics;
import cn.org.matrix.gmatrix.gameLab.control.KeyManager;
/**
* 游戏对话框类
* @author cleverpig
*
*/
public class GameDialogBox extends GameUI implements Runnable{
//描述文字
private Description description=null;
//边缘填充宽度
private int stuffingWidth=0;
//边缘填充高度
private int stuffingHeight=0;
//选择值
private int selectedValue=0;
//定义选择值:确定
public static int OK=1;
//定义选择值:取消
public static int CANCEL=2;
//定义选择值:超时自动处理
public static int TIMEOUT=2;
//正在运行标志
private boolean running=true;
//计时器
private int timer=0;
public GameDialogBox(){
super();
}
/**
* 构造方法
* @param stuffingWidth 边缘填充宽度
* @param stuffingHeight 边缘填充高度
* @param des 描述性文字对象
*/
public GameDialogBox(int stuffingWidth,int stuffingHeight,Description des,
int width,int height,
int startX,int startY,
int affirmKey,int cancelKey,
int type,int bgColor,int uiBgColor,
int borderColor,int waitTime){
super(width,height,startX,startY,affirmKey,cancelKey,type,
bgColor,uiBgColor,borderColor,waitTime);
this.stuffingWidth=stuffingWidth;
this.stuffingHeight=stuffingHeight;
this.description=des;
}
/**
* 绘制图形
*/
public void paint(Graphics g){
//绘制背景
g.setColor(this.getColorScheme().getBgColor());
g.fillRect(0,0,getWidth(),getHeight());
//画出对话框
g.setColor(this.getColorScheme().getUIBgColor());
g.fillRect(this.getScope().getLeftTopCoordinates().getX(),
this.getScope().getLeftTopCoordinates().getY(),
this.getScope().getUIWidth(),this.getScope().getUIHeight());
g.setColor(this.getColorScheme().getUIBorderColor());
g.drawRect(this.getScope().getLeftTopCoordinates().getX(),
this.getScope().getLeftTopCoordinates().getY(),
this.getScope().getUIWidth(),this.getScope().getUIHeight());
//设置字体
g.setFont(description.getFont());
//计算描述性文字的位置
description.setDescriptionPosition(this.getWidth()/2-description.getFont().stringWidth(description.getContent())/2,
this.getScope().getLeftTopCoordinates().getY()+this.getScope().getUIHeight()/2+description.getFont().getHeight()/2);
//画出描述性文字
g.drawString(description.getContent(),
description.getDescriptionPosition().getX(),
description.getDescriptionPosition().getY(),
Graphics.BOTTOM|Graphics.LEFT);
}
/**
* 处理键盘事件
* @param 键盘代码
*/
public void keyPressed(int keyCode){
// System.out.println("系统按键值:"+keyCode);
// System.out.println("用户按下了:"+KeyManager.getKeyValue(keyCode));
// System.out.println("确认按钮值:"+this.getAffirmKey());
// System.out.println("取消按钮值:"+this.getCancelKey());
//根据用户输入的键盘值,赋值给selectedValue
if (KeyManager.getKeyValue(getGameAction(keyCode))==this.getAffirmKey()){
selectedValue=OK;
}
if (KeyManager.getKeyValue(getGameAction(keyCode))==this.getCancelKey()){
selectedValue=CANCEL;
}
}
/**
* 线程的run方法
*/
public void run(){
System.out.println("对话框启动");
while(running){
try{
Thread.sleep(500);
}
catch(Exception ex){
}
//根据对话框类型判断是否选择完毕退出线程
switch(this.getType()){
case GameUI.NEVERCLOSE_UNTILKEYPRESS:
if (selectedValue>0){
running=false;
}
break;
case GameUI.CLOSE_UNTILTIMEOUT:
if (timer>=this.getWaitTime()){
selectedValue=TIMEOUT;
running=false;
}
System.out.println("计时器:"+timer);
timer+=500;
break;
}
}
System.out.println("对话框退出");
}
public Description getDescription() {
return description;
}
public void setDescription(Description description) {
this.description = description;
}
public int getStuffingHeight() {
return stuffingHeight;
}
public void setStuffingHeight(int stuffingHeight) {
this.stuffingHeight = stuffingHeight;
}
public int getStuffingWidth() {
return stuffingWidth;
}
public void setStuffingWidth(int stuffingWidth) {
this.stuffingWidth = stuffingWidth;
}
public void setSelectedValue(int selectedValue) {
this.selectedValue = selectedValue;
}
public int getSelectedValue() {
return selectedValue;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
public int getTimer() {
return timer;
}
public void setTimer(int timer) {
this.timer = timer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -