📄 lookcanvas.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
public class LookCanvas extends GameCanvas implements Runnable
{
//画布数据
private final int WIDTH,HEIGHT,width,height,hy,vx,bandLength,charHeight,vLocation;
private int k;
private String[] str=null;
//记录存放集合变量
private ChessRecord chessRecord=null;
private int[][] chess=null;
private int[] contentIds=null,chessChance=null;
//走步标记
private int chanceNumber=0;
private boolean state=true,stop=false;
//************存放在记录中的供用户进行设置的变量
//背景色彩
private int bgColorR = 0, bgColorG = 0, bgColorB = 0;
//棋盘色彩
private int chessColorR = 0, chessColorG = 0, chessColorB = 0;
//楚河汉界色彩
private int riverColorR = 0, riverColorG = 0, riverColorB = 0;
//计时器色彩
private int timeColorR = 0, timeColorG = 0, timeColorB = 0;
//我方棋子色彩
private int myColorR = 0, myColorG = 0, myColorB = 0;
//对方棋子色彩
private int yourColorR = 0, yourColorG = 0, yourColorB = 0;
//字符色彩
private int fontColorR = 0, fontColorG = 0, fontColorB = 0;
//我方棋子选择后的色彩
private int selectedColorR = 0, selectedColorG = 0, selectedColorB = 0;
//选择框的色彩
private int selectColorR = 0, selectColorG = 0, selectColorB = 0;
//休眠时间,如果反应太快的话,可以手动增加它的值
private int sleepTime = 0;
private RecordManage recordManage = null;
public LookCanvas(RecordManage recordManage, String indexName,ChessRecord cRecord)
{
super(true);
this.recordManage = recordManage;
chessRecord=cRecord;
//获取棋盘初始数组
chess=chessRecord.getChess(indexName);
//System.out.println("成功取得棋盘初始化数组");
//取得走棋步子集合
contentIds=chessRecord.getChessChance();
chanceNumber=contentIds.length-1;
//System.out.println("棋步的数目是:"+chanceNumber+1);
//System.out.println("成功取得棋盘走步集合");
//画布变量初始化
setFullScreenMode(true);
WIDTH=getWidth();
HEIGHT=getHeight();
width=WIDTH/10;
Font f = Font.getDefaultFont();
charHeight = f.getHeight();
height=(HEIGHT - 2*charHeight)/10;
hy = (HEIGHT - 9 * height)/2;
vx = (WIDTH - 8 * width)/2;
bandLength = Math.min(width ,height);
vLocation = vx + width * 4;
//初始化象棋字符
str=new String[33];
str[0]="将";
str[1]="兵";
str[2]="兵";
str[3]="兵";
str[4]="兵";
str[5]="兵";
str[6]="炮";
str[7]="炮";
str[8]="车";
str[9]="车";
str[10]="马";
str[11]="马";
str[12]="相";
str[13]="相";
str[14]="士";
str[15]="士";
str[16]="帅";
str[17]="卒";
str[18]="卒";
str[19]="卒";
str[20]="卒";
str[21]="卒";
str[22]="炮";
str[23]="炮";
str[24]="车";
str[25]="车";
str[26]="马";
str[27]="马";
str[28]="象";
str[29]="象";
str[30]="仕";
str[31]="仕";
str[32]="";
getUserSetup();
//开启画布线程
new Thread(this).start();
}
private void getUserSetup()
{
SetupRecord record = new SetupRecord();
Setup setup = record.getUserSetup();
int[][] setupData = setup.getCanvas();
bgColorR = setupData[0][0];
bgColorG = setupData[0][1];
bgColorB = setupData[0][2];
chessColorR = setupData[1][0];
chessColorG = setupData[1][1];
chessColorB = setupData[1][2];
riverColorR = setupData[2][0];
riverColorG = setupData[2][1];
riverColorB = setupData[2][2];
timeColorR = setupData[3][0];
timeColorG = setupData[3][1];
timeColorB = setupData[3][2];
myColorR = setupData[4][0];
myColorG = setupData[4][1];
myColorB = setupData[4][2];
yourColorR = setupData[5][0];
yourColorG = setupData[5][1];
yourColorB = setupData[5][2];
fontColorR = setupData[6][0];
fontColorG = setupData[6][1];
fontColorB = setupData[6][2];
selectedColorR = setupData[7][0];
selectedColorG = setupData[7][1];
selectedColorB = setupData[7][2];
selectColorR = setupData[8][0];
selectColorG = setupData[8][1];
selectColorB = setupData[8][2];
sleepTime = setup.getSleepTime();
record.closeSetupRecord();
}
public void run()
{
while(!stop)
{
long startTime = System.currentTimeMillis();
int keyState=getKeyStates();
if((keyState&GameCanvas.GAME_A_PRESSED)!=0)
{
stop = true;
Chess.dis.setCurrent(recordManage);
}
if((keyState&GameCanvas.FIRE_PRESSED)!=0)
{
if(chanceNumber<0)
{//下面这种形式会一直停留在Alert界面
/*Alert alert=new Alert("Warning","已经是最后一步了",null,AlertType.WARNING);
Chess.dis.setCurrent(alert,this);*/
}
else
{
//System.out.println("再现记录按键一次");
int tempRecordId=contentIds[chanceNumber];
//System.out.println("绘画前取得tempRecordId是:"+tempRecordId);
chessChance=chessRecord.getChanceRecord(tempRecordId);
int m=chessChance[0];
int n=chessChance[1];
int y=chessChance[2];
int x=chessChance[3];
//System.out.println("取得的两个位置是");
//System.out.println(m+","+n);
//System.out.println(y+","+x);
chess[y][x]=chess[m][n];
chess[m][n]=32;
//System.out.println("变量设置完成");
state=true;
chanceNumber=chanceNumber-1;
//System.out.println("一步完成");
}
}
if(state)
{
//***********************画棋局
//取得画布
Graphics g=getGraphics();
//画出背景
g.setColor(bgColorR,bgColorG,bgColorB);
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(chessColorR,chessColorG,chessColorB);
//画出大矩形
g.drawRect(vx,hy,width * 8 ,height * 9);
//画出上半截竖条
for(int i=1;i<8;i++)
g.drawLine(vx+i*width,hy,vx+i*width,hy+height*4);
g.drawLine(vx+3*width, hy, vx+5*width, hy+2*height);
g.drawLine(vx+5*width, hy, vx+3*width, hy+2*height);
//画出横条
for(int i=1;i<9;i++)
g.drawLine(vx,hy+i*height,vx + width * 8,hy+i*height);
//画出下半截竖条
for(int i=1;i<8;i++)
g.drawLine(vx+i*width,hy+5*height,vx+i*width,hy+9*height);
g.drawLine(vx+3*width, hy+9*height, vx+5*width, hy+7*height);
g.drawLine(vx+3*width, hy+7*height, vx+5*width, hy+9*height);
//画河
g.setColor(riverColorR,riverColorG,riverColorB);
g.fillRect(vx ,hy + height * 4 ,width * 8 + 1,height + 1);
//画出棋子初始摆放位置
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(chess[i][j]!=32)
{
k=chess[i][j];
if(k>15)
g.setColor(myColorR,myColorG,myColorB);
else
g.setColor(yourColorR,yourColorG,yourColorB);
g.fillArc(width*j+vx-width/2,height*i+hy-height/2,width,width,0,360);
g.setColor(fontColorR,fontColorG,fontColorB);
//与画字符有关的,各种差值固定,不随机型改变
g.drawString(str[k],width*j+vx,height*i+hy - charHeight/2,Graphics.TOP|Graphics.HCENTER);
}
}
}
state=false;
}
long endTime = System.currentTimeMillis();
int duration = (int)(endTime - startTime);
if(duration<(sleepTime - 10))
{
try
{
Thread.sleep(sleepTime-duration);
}
catch(InterruptedException ie)
{
}
}
flushGraphics();
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -