📄 hitmouse_midlet.java
字号:
package ch05;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Hitmouse_MIDlet
extends MIDlet
implements CommandListener {
//声明一个Display对象
Display display;
//老鼠出现的事件间隔,用来控制游戏难度
static int time = 3000;
//存储各类图标
Image image;
//提示信息滚动条
Ticker currentTicker;
//游戏选项列表
List frm;
//声明一个退出按钮
Command exitCommand;
//声明一个返回按钮
Command backCommand;
//声明一个停止按钮
Command stopCommand;
//声明一个确认按钮
Command sureCommand;
//声明一个单选按钮组
ChoiceGroup radioChoiceGroup;
//声明一个实现绘制的Canvas类对象
Hitmouse_Canvas lhc;
//窗体
Form fm;
//构造器
public Hitmouse_MIDlet() {
display = Display.getDisplay(this);
}
//启动应用程序
public void startApp() {
display.setCurrent(creatAlect("欢迎进入打老鼠游戏", "/icons/hitmouse/start.png",
"欢迎游戏"));
display.setCurrent(creatList());
fm = creatForm();
}
//创建欢迎提示框
public Alert creatAlect(String content, String imgpath, String title) {
try {
currentTicker = new Ticker("");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Alert currentAlert = new Alert(title);
currentAlert.setString(content);
try {
image = Image.createImage(imgpath);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
currentAlert.setImage(image);
currentAlert.setType(AlertType.ALARM);
currentAlert.setTimeout(3000);
return currentAlert;
}
//创建一个游戏功能选项列表
public List creatList() {
frm = new List("欢迎游戏", List.IMPLICIT);
Image aimage = null, bimage = null, cimage = null;
String str[] = {
"开始新游戏", "难度设定", "成绩统计"};
try {
aimage = Image.createImage("/icons/background/star.png");
bimage = Image.createImage("/icons/background/hand.png");
cimage = Image.createImage("/icons/background/boot.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
//建立一个列表框
Image[] imageArray = {
aimage, bimage, cimage};
for (int i = 0; i < str.length; i++) {
frm.append(str[i], imageArray[i]);
}
exitCommand = new Command("退出", Command.EXIT, 0);
sureCommand = new Command("确定", Command.OK, 1);
frm.addCommand(exitCommand);
frm.addCommand(sureCommand);
frm.setCommandListener(this);
frm.setTicker(currentTicker);
return frm;
}
//创建游戏难度设定窗体
public Form creatForm() {
fm = new Form("难度设定");
backCommand = new Command("返回", Command.BACK, 0);
radioChoiceGroup = new ChoiceGroup("难度设定", ChoiceGroup.EXCLUSIVE);
String[] stringArray1 = {
"初级游戏", "中级游戏", "高级游戏"};
Image aimage = null, bimage = null, cimage = null;
try {
aimage = Image.createImage("/icons/background/star.png");
bimage = Image.createImage("/icons/background/hand.png");
cimage = Image.createImage("/icons/background/boot.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Image[] imageArray = {
aimage, bimage, cimage};
for (int i = 0; i < stringArray1.length; i++) {
radioChoiceGroup.append(stringArray1[i], imageArray[i]);
}
fm.append(radioChoiceGroup);
fm.addCommand(backCommand);
fm.setCommandListener(this);
return fm;
}
//加入画板方法
public Hitmouse_Canvas drawBook() {
if (lhc == null) {
try {
lhc = new Hitmouse_Canvas();
stopCommand = new Command("停止", Command.STOP, 0);
lhc.addCommand(stopCommand);
lhc.setCommandListener(this);
lhc.start();
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
return lhc;
}
//创建一个按钮监听器
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
}
if (c == sureCommand) {
if (frm.isSelected(0)) {
display.setCurrent(drawBook());
}
if (frm.isSelected(1)) {
display.setCurrent(fm);
}
if (frm.isSelected(2)) {
try {
display.setCurrent(creatAlect("共出现" + Hitmouse_Canvas.bornmouse +
"只老鼠," + "恭喜你打中" +
Hitmouse_Canvas.killedmouse +
"只老鼠 ,获得" +
Hitmouse_Canvas.killedmouse * 100 /
Hitmouse_Canvas.bornmouse + "分!",
"/icons/hitmouse/laugh.png", "恭喜"));
}
catch (Exception ex) {
display.setCurrent(creatAlect("抱歉!您还未进行打老鼠游戏!",
"/icons/hitmouse/laugh.png", "抱歉"));
}
Hitmouse_Canvas.bornmouse = 0;
Hitmouse_Canvas.killedmouse = 0;
}
}
if (c == stopCommand) {
display.setCurrent(frm);
}
if (c == backCommand) {
if (radioChoiceGroup.getSelectedIndex() == 0) {
time = 3000;
}
if (radioChoiceGroup.getSelectedIndex() == 1) {
time = 1000;
}
if (radioChoiceGroup.getSelectedIndex() == 2) {
time = 300;
}
display.setCurrent(frm);
}
}
//挂起应用程序
public void pauseApp() {
}
//撤消应用程序
public void destroyApp(boolean unconditional) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -