📄 hui24canvas.java
字号:
/*
* 创建日期 2006-7-13
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package indnx;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
*
*/
public class Hui24Canvas extends Canvas implements Runnable {
public Graphics gx;
public Display display;
private boolean ThreadSTOP = true,over;
private static boolean KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6;
protected static boolean KEY_BIAO ,KEY_SUO;
public boolean FA_PAI;
public static Timer timer;
private static int Num=0;
//存放用户输入公式的顺序代码
protected String[] SHU_RU= new String[11];
public static Image pai, biaoZhi, diSe;
public CaiDanTimer shiJian;
// private char[] gong = new char[10];
private StringBuffer buffer;
protected static int indxe = 1;
private IndnxMIDlet midlet;
protected static int dite, FenShu, dieD , dieT , HP;
//显示的字符
private String displayResult;
//定义4个变量,用来存放随即抽去的牌号。只在调用couQuPai()方法时更改,
public static int ChouQuPai1, ChouQuPai2, ChouQuPai3, ChouQuPai4;
public Tools tools;
//定义一个后屏幕
public Image HouPing;
//定义4个标志量,用来判断当前所用算法
public Hui24Canvas(IndnxMIDlet midlet) {
this.midlet =midlet;
try {
pai = Image.createImage("/pai.png");
biaoZhi = Image.createImage("/biaoZhi.png");
diSe = Image.createImage("/diSe.png");
HouPing = Image.createImage(getWidth(), getHeight());
} catch (IOException e) {
e.printStackTrace();
}
indxe=1;
over=false;KEY_SUO=true;KEY_1=true;KEY_2=true;KEY_3=true;KEY_4=true;KEY_5=true;KEY_6=true;KEY_BIAO = true;FA_PAI=true;
HP = 5;
FenShu = 0;
dite = 0;dieD = 60; dieT = 1;
gx = HouPing.getGraphics();
buffer=new StringBuffer();
timer = new Timer();
shiJian = new CaiDanTimer();
// new Thread(this).start();
timer.scheduleAtFixedRate(shiJian, 0, 1000);
tools = new Tools();
couQuPai();
tools.run24(ChouQuPai1, ChouQuPai2, ChouQuPai3, ChouQuPai4);
// for(int i=0;i<8;i++){
// System.out .println("GongShi : "+tools.GongShi[i] ) ;
// }
}
/**
*
*/
public static final int INDNX_1 = 1, INDNX_2 = 2, INDNX_3 = 3, INDNX_4 = 4;
protected void paint(Graphics g) {
jieMian(gx);
g.drawImage(HouPing, 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void run() {
// while(ThreadSTOP){
// long startTime = System.currentTimeMillis();
// jinDuTiao(gx);
// long timeTake = System.currentTimeMillis() - startTime;
// }
}
private void jieMian(Graphics gx) {
gx.setColor(0x000000);
gx.fillRect(0, 0, getWidth(), getHeight());
gx.setColor(0xFF0101);
gx.drawRect(0, 0, 64, 15);
gx.setColor(0xFF0101);
gx.fillRect(2, 2, dite / 2, 12);
gx.setColor(0xF6CF5A);
gx.drawString(dieT + ":" + dieD, 25, 1, Graphics.TOP | Graphics.LEFT);
gx.setColor(0xF6CF5A);
gx.drawString("分数 :" + FenShu, 70, 1, Graphics.TOP | Graphics.LEFT);
beiJingSe(gx);
lid(gx);
displayResult=buffer.toString() ;
System.out.println("displayResult"+displayResult);
switch (indxe) {
case 1:
gx.setColor(0xffffff);
gx.fillRect(22, 100, 86, 19);
gx.setColor(0xF6CF5A);
gx.drawRect(24, 102, 82, 15);
gx.drawString(" 请在下面输入公式", 14, 80, Graphics.TOP | Graphics.LEFT);
gx.setColor(0x000000);
gx.drawString(displayResult, 26, 101, Graphics.TOP | Graphics.LEFT);
break;
case 2:
gx.setClip(48, 80, 32, 32);
gx.drawImage(biaoZhi, 48, 48, Graphics.TOP | Graphics.LEFT);
gx.setClip(0, 0, 128, 128);
break;
case 3:
gx.setClip(48, 80, 32, 32);
gx.drawImage(biaoZhi, 48, 16, Graphics.TOP | Graphics.LEFT);
gx.setClip(0, 0, 128, 128);
}
if(over)Exit();
}
//绘出时间条
private final void jinDuTiao(Graphics gx) {
dite++;
dieD--;
if (dieD == 0 && dieT > 0) {
dieT--;
dieD = 60;
}
if (dite == 120) {
FenShu-=20;
if(FenShu<0){
over=true;
repaint();
// FA_PAI=false;
shiJian.cancel() ;
KEY_BIAO = false;}
}
}
//重新洗牌
public void RunTimer() {
buffer=null;
buffer=new StringBuffer();
// FA_PAI=false;
tools.JieGuo="";
dite = 0;
dieD = 60;
dieT = 1;
KEY_1=true;KEY_2=true;KEY_3=true;KEY_4=true;KEY_SUO=true;KEY_5=true;KEY_6=true;
// timer.scheduleAtFixedRate(shiJian, 0, 1000);
couQuPai();
tools.run24(ChouQuPai1, ChouQuPai2, ChouQuPai3, ChouQuPai4);
indxe=1;
repaint();
}
private static final void beiJingSe(Graphics gx) {
gx.drawImage(diSe, 0, 17, Graphics.TOP | Graphics.LEFT);
gx.setColor(0xffffff);
gx.fillRoundRect(4, 27, 27, 42, 5, 5);
gx.fillRoundRect(35, 27, 27, 42, 5, 5);
gx.fillRoundRect(66, 27, 27, 42, 5, 5);
gx.fillRoundRect(97, 27, 27, 42, 5, 5);
}
protected final static void tiePai(Graphics gx, int x1, int x2, int x3,
int x4) {
gx.setClip(5, 28, 25, 40);
gx.drawImage(pai, x1, 28, Graphics.TOP | Graphics.LEFT);
gx.setClip(36, 28, 25, 40);
gx.drawImage(pai, x2, 28, Graphics.TOP | Graphics.LEFT);
gx.setClip(67, 28, 25, 40);
gx.drawImage(pai, x3, 28, Graphics.TOP | Graphics.LEFT);
gx.setClip(98, 28, 25, 40);
gx.drawImage(pai, x4, 28, Graphics.TOP | Graphics.LEFT);
gx.setClip(0, 0, 128, 128);
}
private void couQuPai() {
ChouQuPai1 = 0;
ChouQuPai2 = 0;
ChouQuPai3 = 0;
ChouQuPai4 = 0;
ChouQuPai1 = tools.suiJi();
ChouQuPai2 = tools.suiJi();
ChouQuPai3 = tools.suiJi();
ChouQuPai4 = tools.suiJi();
}
protected void Exit(){
Graphics gx=HouPing.getGraphics();
gx.setColor(0x000000);
gx.drawRect(30,50,70,28);
gx.setColor(0xF6CF5A);
gx.fillRect(31,51,68,26);
gx.setColor(0xFF0101);
gx.drawString("Game oveR",36,56, Graphics.TOP | Graphics.LEFT);
}
protected void tuiChu() {
tools=null;
buffer=null;
timer=null;
shiJian.cancel();
shiJian =null;
System.gc();
}
private int JiaIndxn1 = 0,JiaIndxn2=0;
public static final void lid(Graphics gx) {
int t1, t2, t3, t4;
t1 = 5 - 25 * (ChouQuPai1 - 1);
t2 = 36 - 25 * (ChouQuPai2 - 1);
t3 = 67 - 25 * (ChouQuPai3 - 1);
t4 = 98 - 25 * (ChouQuPai4 - 1);
tiePai(gx, t1, t2, t3, t4);
}
protected void NUM1() {
int key = ChouQuPai1;
buffer.append(key);
SHU_RU[Num]=Integer.toString(ChouQuPai1);
JiaIndxn2=0;
Num++;
KEY_1=false;
}
protected void NUM2() {
int key = ChouQuPai2;
buffer.append(key);
SHU_RU[Num]=Integer.toString(ChouQuPai2);
JiaIndxn2=0;
Num++;
KEY_2=false;
}
protected void NUM3() {
int key = ChouQuPai3;
buffer.append(key);
SHU_RU[Num]=Integer.toString(ChouQuPai3);
JiaIndxn2=0;
Num++;
KEY_3=false;
}
protected void NUM4() {
int key = ChouQuPai4;
buffer.append(key);
SHU_RU[Num]=Integer.toString(ChouQuPai4);
JiaIndxn2=0;
Num++;
KEY_4=false;
}
protected void NUM5() {
if (JiaIndxn1 % 2 == 0){
buffer.append("(");
SHU_RU[Num]="(";
Num++;
}
else
{buffer.append(")");
SHU_RU[Num]=")";
Num++;}
JiaIndxn1++;
}
protected void NUM6() {
switch (JiaIndxn2 % 4) {
case 0:
buffer.append("+");
SHU_RU[Num]="+";
Num++;
break;
case 1:
int tik=buffer.length() ;
tik--;Num--;
buffer.deleteCharAt(tik--);
buffer.append("-");
SHU_RU[Num]="-";
Num++;
break;
case 2:
tik=buffer.length() ;
tik--;Num--;
buffer.deleteCharAt(tik--);
buffer.append("*");
SHU_RU[Num]="*";
Num++;
break;
case 3:
tik=buffer.length() ;
tik--;Num--;
buffer.deleteCharAt(tik--);
buffer.append("/");
SHU_RU[Num]="/";
Num++;
break;
}
JiaIndxn2++;
// System.out.println("JiaIndxn1 "+JiaIndxn1);
}
//POUND:# 否定
protected void POUND() {
if(KEY_SUO){
// FA_PAI=true;
try{
if (tools.JieGuo.equals("")) {
FenShu += 10;
indxe = 2;
// System.out.println("在这里");
} else {
FenShu-=15;
if(FenShu<0){
over=true;
repaint();
// FA_PAI=false;
shiJian.cancel() ;
// KEY_BIAO = false;
}
indxe = 3;
}}catch (Exception e){}KEY_6=false;
}else{
// System.out.println(" buffer.length = "+buffer.length());
int tik=buffer.length() ;
tik--;Num--;
buffer.deleteCharAt(tik--);
}
}
//STAR:* 确定
protected void STAR() {
// FA_PAI=true;
String[] Shu_Ru=new String[Num];
for(int i=0;i<Num ;i++){
Shu_Ru[i]=SHU_RU[i];
}
String JIE_GUO=tools.jiSuan(displayResult,Shu_Ru);
if (JIE_GUO.equals("24")) {
FenShu += 20;
indxe = 2;
} else {
FenShu-=15;
if(FenShu<0){
over=true;
repaint();
// FA_PAI=false;
shiJian.cancel() ;
// KEY_BIAO = false;
}
indxe = 3;
}Num=0;
// FA_PAI=true;
KEY_5=false;
}
protected void keyPressed(int keyCode) {
int game = keyCode;
// System.out.println("game = "+game+" KEYNUM1 = "+Canvas.KEY_NUM1);
switch (game) {
case Canvas.KEY_NUM1:
// System.out.println("in KEY_NUM1 = "+KEY_BIAO);
if (KEY_BIAO&&KEY_1) {
// System.out.println("in keybiao = "+KEY_BIAO);
NUM1();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
case KEY_NUM2:
if (KEY_BIAO&&KEY_2) {
NUM2();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
case KEY_NUM3:
if (KEY_BIAO&&KEY_3) {
NUM3();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
case KEY_NUM4:
if (KEY_BIAO&&KEY_4) {
NUM4();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
case KEY_POUND:
// System.out.println(KEY_6+" KEY_SUO "+KEY_SUO);
if (KEY_BIAO&&KEY_6) {
POUND();
KEY_BIAO = false;
}
break;
case KEY_STAR:
if (KEY_BIAO&&KEY_5) {
STAR();
KEY_BIAO = false;
}
break;
case KEY_NUM5:
if (KEY_BIAO) {
NUM5();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
case KEY_NUM6:
if (KEY_BIAO) {
NUM6();
indxe = 1;
KEY_BIAO = false;
KEY_SUO = false;
}
break;
}
repaint(0,70,128,128);
}
protected void keyReleased(int keyCode) {
int game = keyCode;
switch (game) {
case KEY_NUM1:
KEY_BIAO = true;
break;
case KEY_NUM2:
KEY_BIAO = true;
break;
case KEY_NUM3:
KEY_BIAO = true;
break;
case KEY_NUM4:
KEY_BIAO = true;
break;
case KEY_POUND:
KEY_BIAO = true;
break;
case KEY_STAR:
KEY_BIAO = true;
break;
case KEY_NUM5:
KEY_BIAO = true;
break;
case KEY_NUM6:
KEY_BIAO = true;
break;
}
if(over){KEY_BIAO=false;
FA_PAI=false;
}
}
private void jiSuan24(){
}
class CaiDanTimer extends TimerTask {
public void run() {
jinDuTiao(gx);
repaint(0,0,128,15);
// repaint(0,70,128,128);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -