import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChessBoard extends JPanel implements MouseListener,
MouseMotionListener {
public ChessPoint point[][];
public int unitWidth, unitHeight;
private int xlength, ylength;
private int x, y;
private Image img;
protected Image pieceImg;
private boolean move = false;
public String redcolor = "red", blackcolor = "black";
ChessPiece redcar1, redcar2, redhorse1, redhorse2, redchanceor1,redchanceor2, redgeneral, redguard1, redguard2, redsoldier1, redsoldier2, redsoldier3, redsoldier4,
redsoldier5, redcannon1, redcannon2;
ChessPiece blackcar1, blackcar2, blackhorse1, blackhorse2, blackgeneral,blackguard1, blackguard2,blacksoldier1, blacksoldier2, blacksoldier3, blacksoldier4, blacksoldier5, blackchanceor1,
blackchanceor2, blackcannon1,blackcannon2;
int startX, startY;
int startI, startJ;
public boolean redmovepiece = true, blackmovepiece = false;
Rule rule = null;
public MakeChessManual record = null;
public ChessBoard(int w, int h, int r, int c) {
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
Color bc = getBackground();
unitWidth = w;
unitHeight = h;
xlength = r;
ylength = c;
point = new ChessPoint[r + 1][c + 1];
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
point[i][j] = new ChessPoint(i * unitWidth, j * unitHeight,
false);
}
}
rule = new Rule(this, point);
record = new MakeChessManual(this, point);
img = Toolkit.getDefaultToolkit().getImage("board.jpg");
pieceImg = Toolkit.getDefaultToolkit().getImage("board.png");
redcar1 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
redcar1.setpiecesype(redcolor);
redcar2 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
redcar2.setpiecesype(redcolor);
redhorse1 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
redhorse1.setpiecesype(redcolor);
redhorse2 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
redhorse2.setpiecesype(redcolor);
redcannon1 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
redcannon1.setpiecesype(redcolor);
redcannon2 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
redcannon2.setpiecesype(redcolor);
redchanceor1 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
redchanceor1.setpiecesype(redcolor);
redchanceor2 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
redchanceor2.setpiecesype(redcolor);
redguard1 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
redguard1.setpiecesype(redcolor);
redguard2 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
redguard2.setpiecesype(redcolor);
redgeneral = new ChessPiece("帅", Color.red, bc, w - 4, h - 4, this);
redgeneral.setpiecesype(redcolor);
redsoldier1 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
redsoldier1.setpiecesype(redcolor);
redsoldier2 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
redsoldier2.setpiecesype(redcolor);
redsoldier3 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
redsoldier3.setpiecesype(redcolor);
redsoldier4 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
redsoldier4.setpiecesype(redcolor);
redsoldier5 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
redsoldier5.setpiecesype(redcolor);
blackgeneral = new ChessPiece("将", Color.black, bc, w - 4, h - 4, this);
blackgeneral.setpiecesype(blackcolor);
blackguard1 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
blackguard1.setpiecesype(blackcolor);
blackguard2 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
blackguard2.setpiecesype(blackcolor);
blackcar1 = new ChessPiece("车", Color.black, bc, w - 4, h - 4, this);
blackcar1.setpiecesype(blackcolor);
blackcar2 = new ChessPiece("车", Color.black, bc, w - 4, h - 4, this);
blackcar2.setpiecesype(blackcolor);
blackcannon1 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
blackcannon1.setpiecesype(blackcolor);
blackcannon2 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
blackcannon2.setpiecesype(blackcolor);
blackchanceor1 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
blackchanceor1.setpiecesype(blackcolor);
blackchanceor2 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
blackchanceor2.setpiecesype(blackcolor);
blackhorse1 = new ChessPiece("马", Color.black, bc, w - 4, h - 4, this);
blackhorse1.setpiecesype(blackcolor);
blackhorse2 = new ChessPiece("马", Color.black, bc, w - 4, h - 4, this);
blackhorse2.setpiecesype(blackcolor);
blacksoldier1 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
blacksoldier1.setpiecesype(blackcolor);
blacksoldier2 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
blacksoldier2.setpiecesype(blackcolor);
blacksoldier3 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
blacksoldier3.setpiecesype(blackcolor);
blacksoldier4 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
blacksoldier4.setpiecesype(blackcolor);
blacksoldier5 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
blacksoldier5.setpiecesype(blackcolor);
point[1][10].setPiece(redcar1, this);
point[2][10].setPiece(redhorse1, this);
point[3][10].setPiece(redchanceor1, this);
point[4][10].setPiece(redguard1, this);
point[5][10].setPiece(redgeneral, this);
point[6][10].setPiece(redguard2, this);
point[7][10].setPiece(redchanceor2, this);
point[8][10].setPiece(redhorse2, this);
point[9][10].setPiece(redcar2, this);
point[2][8].setPiece(redcannon1, this);
point[8][8].setPiece(redcannon2, this);
point[1][7].setPiece(redsoldier1, this);
point[3][7].setPiece(redsoldier2, this);
point[5][7].setPiece(redsoldier3, this);
point[7][7].setPiece(redsoldier4, this);
point[9][7].setPiece(redsoldier5, this);
point[1][1].setPiece( blackcar1, this);
point[2][1].setPiece(blackhorse1, this);
point[3][1].setPiece(blackchanceor1, this);
point[4][1].setPiece(blackguard1, this);
point[5][1].setPiece(blackgeneral, this);
point[6][1].setPiece(blackguard2, this);
point[7][1].setPiece(blackchanceor2, this);
point[8][1].setPiece(blackhorse2, this);
point[9][1].setPiece(blackcar2, this);
point[2][3].setPiece(blackcannon1, this);
point[8][3].setPiece(blackcannon2, this);
point[1][4].setPiece(blacksoldier1, this);
point[3][4].setPiece(blacksoldier2, this);
point[5][4].setPiece(blacksoldier3, this);
point[7][4].setPiece(blacksoldier4, this);
point[9][4].setPiece(blacksoldier5, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img,27,32, null);
for (int i = 1; i <= xlength; i++) {
g.drawString("" + i, i * unitWidth, unitHeight / 2);
}
int j = 1;
for (char c = 'A'; c <= 'J'; c++) {
g.drawString("" + c, unitWidth / 4, j * unitHeight);
j++;
}
}
/**鼠标按下事件*/
public void mousePressed(MouseEvent e) {
ChessPiece piece = null;
Rectangle rect = null;
if (e.getSource() == this)
move = false;
if (move == false)
if (e.getSource() instanceof ChessPiece) {
piece = (ChessPiece) e.getSource();
startX = piece.getBounds().x;
startY = piece.getBounds().y;
rect = piece.getBounds();
for (int i = 1; i <= xlength; i++) {
for (int j = 1; j <= ylength; j++) {
int x = point[i][j].getX();
int y = point[i][j].getY();
if (rect.contains(x, y)) {
startI = i;
startJ = j;
break;
}
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
/**鼠标拖动事件*/
public void mouseDragged(MouseEvent e) {
ChessPiece piece = null;
if (e.getSource() instanceof ChessPiece) {
piece = (ChessPiece) e.get
评论0