📄 hitmouse_canvas.java
字号:
package ch05;
import java.util.*;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Hitmouse_Canvas
extends GameCanvas {
//获取绘制上下文
Graphics g = getGraphics();
//创建一个由于控制老鼠出现的随机对象
Random ra = new Random();
//获取屏幕宽度和高度
int w = getWidth();
int h = getHeight();
//声明一个代表老鼠出现区域的变量
int fang; //取值1~9
//声明一个代表老鼠出现次数的变量
static int bornmouse;
//声明一个代表打中老鼠次数的变量
static int killedmouse;
//声明一个控制绘制的Timer对象
private Timer tm;
//声明一个实现绘制的TimerTask对象
private TimerTask tt;
//构造器
public Hitmouse_Canvas() throws IOException {
super(false);
}
//启动线程方法
public void start() {
//启动使老鼠出现和擦除的线程
tm = new Timer();
tt = new Benben();
tm.scheduleAtFixedRate(tt, 0, 1000);
}
//创建一个任务器
public class Benben
extends TimerTask {
public void run() {
drawBack();
drawArea();
try {
Thread.sleep(Hitmouse_MIDlet.time);
}
catch (Exception e) {
e.printStackTrace();
}
bornmouse++;
}
}
//填充背景
public void drawBack() {
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
flushGraphics();
}
//画板上绘制图形
private void drawArea() {
//随机选取一个区域
int in = ( (ra.nextInt() >>> 1) % 3);
int it = ( (ra.nextInt() >>> 1) % 3);
if (in == 0 & it == 0) {
fang = 1;
}
if (in == 1 & it == 0) {
fang = 2;
}
if (in == 2 & it == 0) {
fang = 3;
}
if (in == 0 & it == 1) {
fang = 4;
}
if (in == 1 & it == 1) {
fang = 5;
}
if (in == 2 & it == 1) {
fang = 6;
}
if (in == 0 & it == 2) {
fang = 7;
}
if (in == 1 & it == 2) {
fang = 8;
}
if (in == 2 & it == 2) {
fang = 9;
}
//画圆老鼠洞方法
g.setColor(0xffffff);
g.fillArc(in * (w / 3), it * (h / 3), w / 3, h / 3, 20, 360);
drawMouse(in * (w / 3), it * (h / 3));
flushGraphics();
}
//画老鼠方法
private void drawMouse(int x, int y) {
g.setColor(0x000000);
g.fillArc(12 + x, 5 + y, 15, 15, 20, 360);
g.fillArc(35 + x, 5 + y, 15, 15, 20, 360);
g.fillArc(17 + x, 15 + y, 28, 28, 20, 360);
g.setColor(0xff00ff);
g.fillArc(25 + x, 21 + y, 7, 7, 20, 360);
g.fillArc(35 + x, 21 + y, 7, 7, 20, 360);
g.setColor(0x00ff00);
g.fillArc(28 + x, 33 + y, 10, 5, 20, 360);
flushGraphics();
}
//画一个被打中后的老鼠
public void drawNewMouse(int x, int y) {
g.setColor(0x00ff00);
g.fillArc(x, y, w / 3, h / 3, 20, 360);
drawMouse(x, y);
g.setColor(0xff0000);
g.fillRect(x + 25, y + 8, 15, 18);
g.setColor(0xff00ff);
g.fillArc(x + 25, y + 8, 15, 5, 20, 360);
g.setColor(0xff00ff);
g.fillArc(x + 25, y + 23, 15, 5, 20, 360);
g.setColor(0xff00ff);
g.fillRect(x + 40, y + 15, 30, 8);
g.setColor(0xffff00);
g.fillArc(x + 55, y + 15, 3, 8, 20, 360);
flushGraphics();
}
//响应按键事件
protected void keyPressed(int key) {
switch (key) {
case KEY_NUM1:
if (fang == 1) {
drawNewMouse(0, 0);
killedmouse++;
}
break;
case KEY_NUM2:
if (fang == 2) {
drawNewMouse(w / 3, 0);
killedmouse++;
}
break;
case KEY_NUM3:
if (fang == 3) {
drawNewMouse(w / 3 * 2, 0);
killedmouse++;
}
break;
case KEY_NUM4:
if (fang == 4) {
drawNewMouse(0, h / 3);
killedmouse++;
}
break;
case KEY_NUM5:
if (fang == 5) {
drawNewMouse(w / 3, h / 3);
killedmouse++;
}
break;
case KEY_NUM6:
if (fang == 6) {
drawNewMouse(w / 3 * 2, h / 3);
killedmouse++;
}
break;
case KEY_NUM7:
if (fang == 7) {
drawNewMouse(0, h / 3 * 2);
killedmouse++;
}
break;
case KEY_NUM8:
if (fang == 8) {
drawNewMouse(w / 3, h / 3 * 2);
killedmouse++;
}
break;
case KEY_NUM9:
if (fang == 9) {
drawNewMouse(w / 3 * 2, h / 3 * 2);
killedmouse++;
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -