forked from gjy03/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessboard.java
More file actions
121 lines (97 loc) · 4.2 KB
/
Chessboard.java
File metadata and controls
121 lines (97 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package view;
import model.ChessColor;
import model.ChessComponent;
import model.EmptySlotComponent;
import model.RookChessComponent;
import controller.ClickController;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* 这个类表示面板上的棋盘组件对象
*/
public class Chessboard extends JComponent {
/**
* CHESSBOARD_SIZE: 棋盘是8 * 8的
* <br>
* BACKGROUND_COLORS: 棋盘的两种背景颜色
* <br>
* chessListener:棋盘监听棋子的行动
* <br>
* chessboard: 表示8 * 8的棋盘
* <br>
* currentColor: 当前行棋方
*/
private static final int CHESSBOARD_SIZE = 8;
private final ChessComponent[][] chessComponents = new ChessComponent[CHESSBOARD_SIZE][CHESSBOARD_SIZE];
private ChessColor currentColor = ChessColor.BLACK;
//all chessComponents in this chessboard are shared only one model controller
private final ClickController clickController = new ClickController(this);
private final int CHESS_SIZE;
public Chessboard(int width, int height) {
setLayout(null); // Use absolute layout.
setSize(width, height);
CHESS_SIZE = width / 8;
System.out.printf("chessboard size = %d, chess size = %d\n", width, CHESS_SIZE);
initiateEmptyChessboard();
// FIXME: Initialize chessboard for testing only.
initRookOnBoard(0, 0, ChessColor.BLACK);
initRookOnBoard(0, CHESSBOARD_SIZE - 1, ChessColor.BLACK);
initRookOnBoard(CHESSBOARD_SIZE - 1, 0, ChessColor.WHITE);
initRookOnBoard(CHESSBOARD_SIZE - 1, CHESSBOARD_SIZE - 1, ChessColor.WHITE);
}
public ChessComponent[][] getChessComponents() {
return chessComponents;
}
public ChessColor getCurrentColor() {
return currentColor;
}
public void putChessOnBoard(ChessComponent chessComponent) {
int row = chessComponent.getChessboardPoint().getX(), col = chessComponent.getChessboardPoint().getY();
if (chessComponents[row][col] != null) {
remove(chessComponents[row][col]);
}
add(chessComponents[row][col] = chessComponent);
}
public void swapChessComponents(ChessComponent chess1, ChessComponent chess2) {
// Note that chess1 has higher priority, 'destroys' chess2 if exists.
if (!(chess2 instanceof EmptySlotComponent)) {
remove(chess2);
add(chess2 = new EmptySlotComponent(chess2.getChessboardPoint(), chess2.getLocation(), clickController, CHESS_SIZE));
}
chess1.swapLocation(chess2);
int row1 = chess1.getChessboardPoint().getX(), col1 = chess1.getChessboardPoint().getY();
chessComponents[row1][col1] = chess1;
int row2 = chess2.getChessboardPoint().getX(), col2 = chess2.getChessboardPoint().getY();
chessComponents[row2][col2] = chess2;
chess1.repaint();
chess2.repaint();
}
public void initiateEmptyChessboard() {
for (int i = 0; i < chessComponents.length; i++) {
for (int j = 0; j < chessComponents[i].length; j++) {
putChessOnBoard(new EmptySlotComponent(new ChessboardPoint(i, j), calculatePoint(i, j), clickController, CHESS_SIZE));
}
}
}
public void swapColor() {
currentColor = currentColor == ChessColor.BLACK ? ChessColor.WHITE : ChessColor.BLACK;
}
private void initRookOnBoard(int row, int col, ChessColor color) {
ChessComponent chessComponent = new RookChessComponent(new ChessboardPoint(row, col), calculatePoint(row, col), color, clickController, CHESS_SIZE);
chessComponent.setVisible(true);
putChessOnBoard(chessComponent);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
private Point calculatePoint(int row, int col) {
return new Point(col * CHESS_SIZE, row * CHESS_SIZE);
}
public void loadGame(List<String> chessData) {
chessData.forEach(System.out::println);
}
}
//