您的位置:首页 > 编程语言

郭克华手机编程教学视频----我的练习源码(21)案例:快手

2009-03-10 13:57 585 查看
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lession14;

import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.midlet.*;

/**
* 案例:快手
* 界面每隔2秒在随机位置出现随机颜色的数字0-9,要求用户快速按下键
* 初始用户分为5分
* 按对,得1分
* 按错,扣2分
* 不按,扣1分
* 用户分为0分时通知用户输了
* @author mouyong
*/
public class FlashHandMidlet extends MIDlet {

/****************开始界面*****************/
Form frmBegin = new Form("欢迎游戏");
ImageItem imgWelcome=null;
Command cmdBegin = new Command("开始游戏", Command.SCREEN, 1);
Command cmdEnd = new Command("退出游戏", Command.EXIT, 1);
Command cmdPause = new Command("暂停", Command.STOP, 1);
Command cmdStart = new Command("开始", Command.STOP, 1);
/****************游戏界面*****************/
FlashHandCanvas fhc = new FlashHandCanvas();
FlashHandTimerTask fhtt = new FlashHandTimerTask(fhc);
Display dis = Display.getDisplay(this);
Timer timer = null;

public FlashHandMidlet() {
try {
Image img = Image.createImage("/lession14/welcome.jpg");
imgWelcome=new ImageItem("", img, ImageItem.LAYOUT_CENTER, "欢迎图片");
} catch (IOException ex) {
ex.printStackTrace();
}
}



public void startApp() {

dis.setCurrent(frmBegin);
frmBegin.addCommand(cmdBegin);
frmBegin.addCommand(cmdEnd);
frmBegin.append(imgWelcome);
System.out.println ("宽:"+frmBegin.getWidth()+" 高:"+frmBegin.getHeight());

frmBegin.setCommandListener(new CommandListener() {

public void commandAction(Command c, Displayable d) {
if (c == cmdBegin) {
dis.setCurrent(fhc);
timer = new Timer();
timer.schedule(fhtt, new Date());
} else if (c == cmdEnd) {
FlashHandMidlet.this.notifyDestroyed();
}
}
});

fhc.addCommand(cmdEnd);
fhc.addCommand(cmdPause);
fhc.setCommandListener(new CommandListener() {

public void commandAction(Command c, Displayable d) {
if (c == cmdEnd) {
FlashHandMidlet.this.notifyDestroyed();
} else if (c == cmdPause) {

fhtt.flag = false;
fhc.removeCommand(cmdPause);
fhc.addCommand(cmdStart);
} else if (c == cmdStart) {
fhtt.flag = true;
timer=new Timer();
timer.schedule(fhtt, new Date());
fhc.removeCommand(cmdStart);
fhc.addCommand(cmdPause);
}
}
});
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

/**
* 画布:完成画数字的功能
*/
class FlashHandCanvas extends Canvas {

private String displayNum = "0";//准备画在界面上的字符串
private int x = 0;//定位字符串的x坐标
private int y = 0;//定位字符串的y坐标
private int color = 0x000000;//画字符串的颜色
private int score = 5;
private boolean press = true;//判断是否按过键的一个标志

protected void paint(Graphics g) {
//清屏
g.setColor(0xffffff);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
//绘用户得分
g.setColor(0x4d89c2);
g.drawString("用户得分:" + score, this.getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP);

//绘字
if (score > 0) {//如果得分大于0,则绘数字
g.setColor(getColor());
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(getDisplayNum(), getX(), getY(), Graphics.LEFT | Graphics.TOP);
} else {//否则显示你输了的消息
g.setColor(0xff0000);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("你输了!!!", this.getWidth() / 2, this.getHeight() / 2, Graphics.RIGHT | Graphics.TOP);
}
}
/*
*监听:完成对用户操作的评价
*/

protected void keyPressed(int keyCode) {
int key = Integer.parseInt(displayNum);
if ((keyCode - 48) == key) {
score++;//如果按对了,加1分
} else if (!((keyCode - 48) == key)) {
score -= 2;//如果按错了,减2分
}

//将按键标志改为true
this.setPress(true);
}

/**
* @return the displayNum
*/
public String getDisplayNum() {
return displayNum;
}

/**
* @param displayNum the displayNum to set
*/
public void setDisplayNum(String displayNum) {
this.displayNum = displayNum;
}

/**
* @return the x
*/
public int getX() {
return x;
}

/**
* @param x the x to set
*/
public void setX(int x) {
this.x = x;
}

/**
* @return the y
*/
public int getY() {
return y;
}

/**
* @param y the y to set
*/
public void setY(int y) {
this.y = y;
}

/**
* @return the color
*/
public int getColor() {
return color;
}

/**
* @param color the color to set
*/
public void setColor(int color) {
this.color = color;
}

private int getScore() {
return score;
}

private void setScore(int i) {
score = i;
}

/**
* @return the press
*/
public boolean isPress() {
return press;
}

/**
* @param press the press to set
*/
public void setPress(boolean press) {
this.press = press;
}
}

/*
* 时间任务:每秒产生一个随机数,
*一组x,y坐标(字符串的绘画位置)
* 一个颜色值
*/
class FlashHandTimerTask extends TimerTask {

FlashHandCanvas fh = null;
Random rand = new Random();
boolean flag = true;//控制run方法的执行

public FlashHandTimerTask(FlashHandCanvas fh) {
this.fh = fh;
}

public void run() {
while (flag) {
if (!fh.isPress()) {//如果没按键,减1分
fh.setScore(fh.getScore() - 1);
}
if (fh.getScore() <= 0) {
flag = false;//如果得分小于0,则停止重绘
}
//修改画布相关参数
this.fh.setColor(rand.nextInt(0xffff00));
this.fh.setDisplayNum("" + rand.nextInt(10));
this.fh.setX(rand.nextInt(this.fh.getWidth()));
this.fh.setY(rand.nextInt(this.fh.getHeight()));
//重绘
fh.repaint();
//设置为没有按过键
fh.setPress(false);
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐