📄 chessboard.java
字号:
import javax.microedition.lcdui.*;
/**
* 该类描述了棋盘,棋子指示器。用户通过方向键可以在棋盘上移动指示器,
* 通过选择键在指示器的位置摆放棋子。
*/
public class Chessboard extends Canvas {
private static final int MAX_GRID_SIZE = 18;
private static final int BLACK_COLOR = 0x00000000;
private static final int WHITE_COLOR = 0x00FFFFFF;
private static final int BACKGROUND_COLOR = 0x00AFAFAF;
private GobangMIDlet midlet;
private String title; //棋盘标题
private int positionX, positionY; //棋盘的位置
private int width, height; //屏幕的尺寸
private int boardWidth, boardHeight; //棋盘的尺寸
private int cols = 15; //
private int rows = 15;
private int gridSize = 0; //棋盘网格尺寸
private int chessmanSize = 0; //棋子的尺寸
private ChessPoint[][] chessPoints; //棋盘中的棋点数组
private int indicatorCol, indicatorRow; //棋盘中指示器的位置。
//屏幕上显示信息的区域
private int infoAreaX, infoAreaY;
private int infoAreaWidth, infoAreaHeight;
private String info = "";
private boolean myMoveable = false; //当前用户是否能够移动指示器
private int myChessman; //我(当前用户)的棋子
private int otherChessman; //对方的棋子
//构造方法,创建一个棋盘。
public Chessboard(GobangMIDlet midlet, String title, int myChessman) {
this.midlet = midlet;
this.title = title;
this.myChessman = myChessman;
if(myChessman == ChessPoint.BLACK_CHESSMAN) {
otherChessman = ChessPoint.WHITE_CHESSMAN;
}
else {
otherChessman = ChessPoint.BLACK_CHESSMAN;
}
setCommandListener(midlet);
//初始化棋盘参数
width = getWidth();
height = getHeight();
gridSize = (width/cols < height/rows) ? width/cols : height/rows;
gridSize = (gridSize < MAX_GRID_SIZE) ? gridSize : MAX_GRID_SIZE;
chessmanSize = gridSize-2;
boardWidth = (cols-1)*gridSize;
boardHeight = (rows-1)*gridSize;
positionX = (width-boardWidth)/2;
positionY = (height-boardHeight)*3/4;
//初始化信息显示区域参数
infoAreaX = 0;
infoAreaY = 0;
infoAreaWidth = width;
infoAreaHeight = (height-boardHeight)*3/4 - gridSize/2;
//创建并初始化棋盘中的棋点。
chessPoints = new ChessPoint[rows][cols];
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
chessPoints[i][j] = new ChessPoint(positionX+j*gridSize, positionY+i*gridSize);
}
}
//初始化指示器的位置
indicatorCol = 0;
indicatorRow = 0;
}
protected void paint(Graphics g) {
clear(g);
drawChessboard(g);
drawChessman(g);
drawIndicator(g);
drawInformation(g);
}
//绘制棋盘
private void drawChessboard(Graphics g) {
g.setColor(BLACK_COLOR);
for(int i=0; i<rows; i++) {//水平线
g.drawLine(positionX, positionY+i*gridSize, positionX+boardWidth, positionY+i*gridSize);
}
for(int j=0; j<rows; j++) {//垂直线
g.drawLine(positionX+j*gridSize, positionY, positionX+j*gridSize, positionY+boardHeight);
}
}
//绘制棋子
private void drawChessman(Graphics g) {
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
if(chessPoints[i][j].getChessman() == ChessPoint.NONE_CHESSMAN) {
continue;
}
if(chessPoints[i][j].getChessman() == ChessPoint.BLACK_CHESSMAN) {
g.setColor(BLACK_COLOR);
}
else if(chessPoints[i][j].getChessman() == ChessPoint.WHITE_CHESSMAN) {
g.setColor(WHITE_COLOR);
}
g.fillArc(chessPoints[i][j].getX()-chessmanSize/2,
chessPoints[i][j].getY()-chessmanSize/2,
chessmanSize, chessmanSize, 0, 360);
}
}
}
//绘制指示器
private void drawIndicator(Graphics g) {
int x = chessPoints[indicatorRow][indicatorCol].getX()-chessmanSize/2;
int y = chessPoints[indicatorRow][indicatorCol].getY()-chessmanSize/2;
g.setColor(BLACK_COLOR);
g.drawLine(x, y, x+2, y);
g.drawLine(x, y, x, y+2);
g.drawLine(x+chessmanSize-2, y, x+chessmanSize, y);
g.drawLine(x+chessmanSize, y, x+chessmanSize, y+2);
g.drawLine(x, y+chessmanSize-2, x, y+chessmanSize);
g.drawLine(x, y+chessmanSize, x+2, y+chessmanSize);
g.drawLine(x+chessmanSize-2, y+chessmanSize, x+chessmanSize, y+chessmanSize);
g.drawLine(x+chessmanSize, y+chessmanSize-2, x+chessmanSize, y+chessmanSize);
}
//在屏幕上绘制信息
private void drawInformation(Graphics g) {
g.setClip(infoAreaX, infoAreaY, infoAreaWidth, infoAreaHeight);
clear(g);
g.setColor(BLACK_COLOR);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString(title, infoAreaX + infoAreaWidth/2, infoAreaY+infoAreaHeight/2, Graphics.HCENTER|Graphics.BOTTOM);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString(info, infoAreaX, infoAreaY+infoAreaHeight, Graphics.LEFT|Graphics.BOTTOM);
}
//清除屏幕
private void clear(Graphics g) {
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, width, height);
}
//设置信息
public void setInformation(String s) {
info = s;
repaint(infoAreaX, infoAreaY, infoAreaWidth, infoAreaHeight);
}
//按键回调方法。
protected void keyReleased(int keyCode) {
if(!myMoveable) {
return;
}
int action = getGameAction(keyCode);
switch(action) {
case DOWN: {
indicatorRow = (indicatorRow+1)%rows;
break;
}
case UP: {
indicatorRow = (indicatorRow-1 + rows)%rows;
break;
}
case LEFT: {
indicatorCol = (indicatorCol-1 + cols)%cols;
break;
}
case RIGHT: {
indicatorCol = (indicatorCol+1)%cols;
break;
}
case FIRE: {
if(chessPoints[indicatorRow][indicatorCol].getChessman() == ChessPoint.NONE_CHESSMAN) {
chessPoints[indicatorRow][indicatorCol].setChessman(myChessman);
myMoveable = false;
updateInfo();
//int x = 14-indicatorCol;
//int y = 14-indicatorRow;
//btconn.send(x, y);
midlet.notifyOther(indicatorCol, indicatorRow); //通知对方
}
break;
}
}
repaint();
}
//开始对弈
public void startPlay() {
if(myChessman == ChessPoint.WHITE_CHESSMAN) {
myMoveable = true;
}
updateInfo();
}
//结束对弈
public void endPlay() {
myMoveable = false;
}
private void updateInfo() {
if(myMoveable) {
setInformation("请您走子......");
}
else {
setInformation("对方正在走子,请您等待......");
}
}
//对方走子
public void otherPlayChessman(int x, int y) {
indicatorCol = x;
indicatorRow = y;
if(chessPoints[indicatorRow][indicatorCol].getChessman() == ChessPoint.NONE_CHESSMAN) {
chessPoints[indicatorRow][indicatorCol].setChessman(otherChessman);
myMoveable = true;
}
updateInfo();
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -