?? gamecanvas.java
字號:
package com.lzw;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GameCanvas extends Canvas implements CommandListener {
protected Game game;
String color = "";
protected int rightSpace;// 屏幕右側留的空間
protected int x;// 棋盤輸出的坐標
private boolean myTurn = false;
protected int gridWidth;// 每個棋格的邊長
protected int mapWidth, canvasW;// 棋盤的寬度和畫布的寬度
protected int a, b, c, d;
protected int chessR;// 棋子的半徑
private int desknum = -1; // 桌子序號
private int seatPos = -1; // 座位序號
private boolean banker = false;
protected int selectedX, selectedY;// 選擇框在棋盤格局上的x,y位置
protected static int i, j;
protected int m, n, p;// 記住開始的selectedX,selectedY和point[selectedX][selectedY]
protected String q;// 記住word[selectedX][selectedY]
protected int guard, guard1, guard2, g, g1;// 標記FIRE被按了多少次,g是用來判斷走直線時前后的棋子,中間是否有其他棋子的累加器
protected static int turnWho;// 表示該誰走了
protected static int isRedWin; // 紅棋勝利
protected static int isWhiteWin; // 白棋勝利
private Client client;
protected Command exitCmd, start, ok;
private int point[][]; // 棋子位置數組
protected String[][] chess; // 棋子名稱數組
private int chessSelColor; // 選擇棋子的顏色
private int backColor; // 棋盤背景色
private int charColor; // 棋子漢字顏色
private int lineColor; // 棋盤線的顏色
private int borderColor;// 楚河漢界的顏色
private int selBorderColor;// 選擇棋子的邊框色
private int blackChees; // 黑棋棋顏色
private int redChess;// 紅棋顏色
public GameCanvas() {
};
public void init() {
// desknum = -1;
// seatPos = -1;
addCommand(start);
addCommand(exitCmd);
initChess();
banker = false;
myTurn = false;
}
public void reset() {
initChess();
addCommand(start);
addCommand(exitCmd);
banker = false;
myTurn = false;
}
public GameCanvas(Game game, Client client) {// 構造函數
this.game = game;
this.client = client;
chessSelColor = 0x188312;// 初始化選擇棋子的顏色
backColor = 0xEECD05; // 初始化背景色
charColor = 0xFFFFFF; // 初始化漢字顏色
lineColor = 0x5A5743; // 初始化棋盤線的顏色
borderColor = 0x5A5743; // 初始化楚河漢界的顏色
selBorderColor = 0x50FAFC; // 初始化選擇棋子的邊框色
blackChees = 0x000000; // 綠棋顏色
redChess = 0xBF0404;// 紅棋顏色
rightSpace = getWidth() / 6;
x = rightSpace * 1 / 3;
canvasW = getWidth() - rightSpace;
mapWidth = canvasW - canvasW % 8;
gridWidth = mapWidth / 8;
a = gridWidth * 2 / 5;
b = gridWidth / 8;
c = gridWidth - a;
d = gridWidth - b;
chessR = gridWidth * 2 / 5;
selectedX = 4;
selectedY = 7;
guard = 0;
guard1 = selectedX;
guard2 = selectedY;
m = guard1;
n = guard2;
chess = new String[10][9];
turnWho = 1;
initChess();
exitCmd = new Command("退出", Command.EXIT, 0);
start = new Command("開始", Command.OK, 1);
addCommand(start);
addCommand(exitCmd);
setCommandListener(this);
}
public void initChess() {
point = new int[][] {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },// 初始化INT數組
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 10, 0, 0, 0, 0, 0, 11, 0 },
{ 12, 0, 13, 0, 14, 0, 15, 0, 16 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 28, 0, 29, 0, 30, 0, 31, 0, 32 },
{ 0, 26, 0, 0, 0, 0, 0, 27, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 17, 18, 19, 20, 21, 22, 23, 24, 25 } };
chess = new String[][] {
{ "車", "馬", "象", "士", "將", "士", "象", "馬", "車", },
{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
{ "空", "炮", "空", "空", "空", "空", "空", "炮", "空", },
{ "卒", "空", "卒", "空", "卒", "空", "卒", "空", "卒", },
{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
{ "卒", "空", "卒", "空", "卒", "空", "卒", "空", "卒", },
{ "空", "炮", "空", "空", "空", "空", "空", "炮", "空", },
{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
{ "車", "馬", "象", "士", "將", "士", "象", "馬", "車", }, };
}
public int getDeskIndex() {
return desknum;
}
public void setDeskIndex(int i) {
desknum = i;
}
public void setSeatPos(int i) {
seatPos = i;
}
public int getSeatPos() {
return seatPos;
}
protected void paintChessboardUp(Graphics g) { // 畫上半部棋盤
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 8; l++) {
g.setColor(lineColor);
g.drawRect(x + l * gridWidth, x + k * gridWidth, gridWidth,
gridWidth);
}
}
g.setColor(lineColor); // 設置畫棋盤線的顏色
g.drawLine(x + 3 * gridWidth, x, x + 5 * gridWidth, x + 2 * gridWidth);
g.drawLine(x + 5 * gridWidth, x, x + 3 * gridWidth, x + 2 * gridWidth);
// 畫左上方的炮
{
g.drawLine(x + d, x + gridWidth + c, x + d, x + gridWidth + d);// 左上豎
g.drawLine(x + c, x + gridWidth + d, x + d, x + gridWidth + d);// 左上橫
g.drawLine(x + d + 2 * b, x + gridWidth + c, x + d + 2 * b, x
+ gridWidth + d);// 右上豎
g.drawLine(x + gridWidth + b, x + gridWidth + d, x + gridWidth + a,
x + gridWidth + d);// 右上橫
g.drawLine(x + d, x + 2 * gridWidth + b, x + d, x + 2 * gridWidth
+ a);// 左下豎
g.drawLine(x + c, x + gridWidth + d + 2 * b, x + d, x + gridWidth
+ d + 2 * b);// 左下橫
g.drawLine(x + d + 2 * b, x + 2 * gridWidth + b, x + d + 2 * b, x
+ 2 * gridWidth + a);// 右下豎
g.drawLine(x + gridWidth + b, x + gridWidth + d + 2 * b, x
+ gridWidth + a, x + gridWidth + d + 2 * b);// 右下橫
}
// 畫右上方的炮
{
g.drawLine(x + d + 6 * gridWidth, x + gridWidth + c, x + d + 6
* gridWidth, x + gridWidth + d);
g.drawLine(x + c + 6 * gridWidth, x + gridWidth + d, x + d + 6
* gridWidth, x + gridWidth + d);
g.drawLine(x + d + 2 * b + 6 * gridWidth, x + gridWidth + c, x + d
+ 2 * b + 6 * gridWidth, x + gridWidth + 13 + 9);
g.drawLine(x + gridWidth + b + 6 * gridWidth, x + gridWidth + d, x
+ gridWidth + a + 6 * gridWidth, x + gridWidth + d);
g.drawLine(x + d + 6 * gridWidth, x + 2 * gridWidth + b, x + d + 6
* gridWidth, x + 2 * gridWidth + a);
g.drawLine(x + c + 6 * gridWidth, x + gridWidth + d + 2 * b, x + d
+ 6 * gridWidth, x + gridWidth + d + 2 * b);
g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 2 * gridWidth + b, x
+ d + 2 * b + 6 * gridWidth, x + 2 * gridWidth + a);
g.drawLine(x + gridWidth + b + 6 * gridWidth, x + gridWidth + d + 2
* b, x + gridWidth + a + 6 * gridWidth, x + gridWidth + d
+ 2 * b);
}
}
protected void paintRiver(Graphics g) {// 畫河
g.setColor(lineColor);
g.drawRect(x, x + 4 * gridWidth, mapWidth, gridWidth);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.setColor(borderColor);
g.drawString("楚河 漢界", getWidth() / 2, x + 4 * gridWidth
+ gridWidth * 3 / 4, Graphics.HCENTER | Graphics.BASELINE);
}
protected void paintChessboardDown(Graphics g) {// 畫下半部棋盤
for (int q = 0; q < 4; q++) {
for (int w = 0; w < 8; w++) {
g.setColor(lineColor);
g.drawRect(x + w * gridWidth, x + (q + 5) * gridWidth,
gridWidth, gridWidth);
}
}
g.setColor(lineColor);
g.drawLine(x + 3 * gridWidth, x + 7 * gridWidth, x + 5 * gridWidth, x
+ 9 * gridWidth);
g.drawLine(x + 5 * gridWidth, x + 7 * gridWidth, x + 3 * gridWidth, x
+ 9 * gridWidth);
// 畫左上方的炮
{
g.drawLine(x + d, x + 6 * gridWidth + c, x + d, x + 6 * gridWidth
+ d);// 左上豎
g.drawLine(x + c, x + 6 * gridWidth + d, x + d, x + 6 * gridWidth
+ d);// 左上橫
g.drawLine(x + d + 2 * b, x + 6 * gridWidth + c, x + d + 2 * b, x
+ 6 * gridWidth + d);// 右上豎
g.drawLine(x + gridWidth + b, x + 6 * gridWidth + d, x + gridWidth
+ a, x + 6 * gridWidth + d);// 右上橫
g.drawLine(x + d, x + 7 * gridWidth + b, x + d, x + 7 * gridWidth
+ a);// 左下豎
g.drawLine(x + c, x + 6 * gridWidth + d + 2 * b, x + d, x + 6
* gridWidth + d + 2 * b);// 左下橫
g.drawLine(x + d + 2 * b, x + 7 * gridWidth + b, x + d + 2 * b, x
+ 7 * gridWidth + a);// 右下豎
g.drawLine(x + gridWidth + b, x + 6 * gridWidth + d + 2 * b, x
+ gridWidth + a, x + 6 * gridWidth + d + 2 * b);// 右下橫
}
// 畫右上方的炮
{
g.drawLine(x + d + 6 * gridWidth, x + 6 * gridWidth + c, x + d + 6
* gridWidth, x + 6 * gridWidth + d);
g.drawLine(x + c + 6 * gridWidth, x + 6 * gridWidth + d, x + d + 6
* gridWidth, x + 6 * gridWidth + d);
g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 6 * gridWidth + c, x
+ d + 2 * b + 6 * gridWidth, x + 6 * gridWidth + d);
g.drawLine(x + gridWidth + b + 6 * gridWidth,
x + 6 * gridWidth + d, x + gridWidth + a + 6 * gridWidth, x
+ 6 * gridWidth + d);
g.drawLine(x + d + 6 * gridWidth, x + 7 * gridWidth + b, x + d + 6
* gridWidth, x + 7 * gridWidth + a);
g.drawLine(x + c + 6 * gridWidth, x + 6 * gridWidth + d + 2 * b, x
+ d + 6 * gridWidth, x + 6 * gridWidth + d + 2 * b);
g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 7 * gridWidth + b, x
+ d + 2 * b + 6 * gridWidth, x + 7 * gridWidth + a);
g.drawLine(x + gridWidth + b + 6 * gridWidth, x + 6 * gridWidth + d
+ 2 * b, x + gridWidth + a + 6 * gridWidth, x + 6
* gridWidth + d + 2 * b);
}
}
protected void paintAllChess(Graphics g) {// 畫出所有棋子
if (point == null)
return;
for (i = 0; i < 10; i++) {
for (j = 0; j < 9; j++) {
if (point[i][j] != 0) {
if (point[i][j] < 17) {
g.setColor(redChess);
} else {
g.setColor(blackChees);
}
g.fillArc(x - chessR + j * gridWidth, x - chessR + i
* gridWidth, 2 * chessR, 2 * chessR, 0, 360);
g.setColor(charColor);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(chess[i][j], x + j * gridWidth, x + chessR + i
* gridWidth, Graphics.HCENTER | Graphics.BOTTOM);
}
}
}
}
protected void paintSelectedChess(Graphics g) {// 畫選擇的棋子
m = guard1;
n = guard2; // 再重新單獨輸出一個棋子
g.setColor(chessSelColor);
g.fillArc(x - chessR + guard1 * gridWidth, x - chessR + guard2
* gridWidth, 2 * chessR, 2 * chessR, 0, 360);
g.setColor(charColor);
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString(chess[guard2][guard1], x + guard1 * gridWidth, x + chessR
+ guard2 * gridWidth, Graphics.HCENTER | Graphics.BOTTOM);
}
protected void whoTurn(Graphics g) {// 判斷該誰走了
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
if (!myTurn) {
g.setColor(0xFE0000);
g.drawString("輪到對家走棋", x, x + chessR + 10 * gridWidth,
Graphics.LEFT | Graphics.BOTTOM);
} else {
g.setColor(0X9F3202);
g.drawString("輪到自己走棋", x, x + chessR + 10 * gridWidth,
Graphics.LEFT | Graphics.BOTTOM);
}
}
protected void checkWin(String str) {// 判斷輸贏
int index = str.indexOf(",");
String str2 = str.substring(index + 1);
try {
if (str2.equals("you")) {
Alert alert;
alert = new Alert("恭喜!", null, Image.createImage(this
.getClass().getResourceAsStream("win.gif")),
AlertType.INFO);
ok = new Command("OK", Command.OK, 1);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(ok);
alert.setCommandListener(this);
Game.display.setCurrent(alert);
} else {
Alert alert = new Alert("遺憾!", null, Image.createImage(this
.getClass().getResourceAsStream("false.gif")),
AlertType.INFO);
ok = new Command("OK", Command.OK, 1);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(ok);
alert.setCommandListener(this);
Game.display.setCurrent(alert);
}
} catch (IOException e) {
e.printStackTrace();
}
}
protected void paintSelected(Graphics g) {// 畫選擇框
g.setColor(selBorderColor);
g.drawRect(x - chessR + selectedX * gridWidth, x - chessR + selectedY
* gridWidth, 2 * chessR, 2 * chessR);
}
protected void paint(Graphics g) {
g.setColor(backColor);
g.fillRect(0, 0, getWidth(), getHeight());
paintChessboardUp(g); // 調用畫上半部棋盤的方法
paintRiver(g); // 調用畫河的方法
paintChessboardDown(g); // 調用畫下半部棋盤的方法
paintAllChess(g); // 調用畫棋子的方法
if (guard % 2 == 1) {
paintSelectedChess(g); // 調用畫選擇棋子的方法
}
paintSelected(g);
whoTurn(g);
}
protected void changTwoChessNum(int m, int n, int selectedX, int selectedY,
boolean send) {// 改變兩個格子的值
if (send) {
if ((color.equals("red") && point[n][m] < 17)
|| (color.equals("white") && point[n][m] >= 17)) {
if (!banker) {
client.sendMessage("move;" + seatPos + ":" + selectedY
+ "," + selectedX + "," + n + "," + m);
} else {
client.sendMessage("move;" + seatPos + ":"
+ (9 - selectedY) + "," + (8 - selectedX) + ","
+ (9 - n) + "," + (8 - m));
}
myTurn = false;
p = point[selectedY][selectedX];
point[selectedY][selectedX] = point[n][m];
point[n][m] = 0;
q = chess[selectedY][selectedX];
chess[selectedY][selectedX] = chess[n][m];
chess[n][m] = "空";
}
} else {
myTurn = false;
p = point[selectedY][selectedX];
point[selectedY][selectedX] = point[n][m];
point[n][m] = 0;
q = chess[selectedY][selectedX];
chess[selectedY][selectedX] = chess[n][m];
chess[n][m] = "空";
}
}
private void theRuleOfChe(int m, int n, int sx, int sy) {// 車的規則
g = 0;
if (m == sx) {
if (n > sy) {
for (i = 1; i < n - sy; i++) {
if (point[sy + i][m] != 0) {
g++;
}
}
} else {
for (i = 1; i < sy - n; i++) {
if (point[n + i][m] != 0) {
g++;
}
}
}
if (g == 0) {
changTwoChessNum(m, n, sx, sy, true);
}
}
if (n == sy) {
if (m > sx) {
for (i = 1; i < m - sx; i++) {
if (point[n][i + sx] != 0) {
g++;
}
}
} else {
for (i = 1; i < sx - m; i++) {
if (point[n][m + i] != 0) {
g++;
}
}
}
if (g == 0) {
changTwoChessNum(m, n, sx, sy, true);
}
}
}
private void theRuleOfMa(int m, int n, int sx, int sy) {// 馬的規則
if (n < 9) {
if (point[n + 1][m] == 0) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -