forked from gjy03/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessComponent.java
More file actions
125 lines (107 loc) · 4.67 KB
/
ChessComponent.java
File metadata and controls
125 lines (107 loc) · 4.67 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
122
123
124
125
package model;
import view.ChessboardPoint;
import controller.ClickController;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.IOException;
/**
* 这个类是一个抽象类,主要表示8*8棋盘上每个格子的棋子情况,当前有两个子类继承它,分别是EmptySlotComponent(空棋子)和RookChessComponent(车)。
*/
public abstract class ChessComponent extends JComponent {
/**
* CHESSGRID_SIZE: 主要用于确定每个棋子在页面中显示的大小。
* <br>
* 在这个设计中,每个棋子的图案是用图片画出来的(由于国际象棋那个棋子手动画太难了)
* <br>
* 因此每个棋子占用的形状是一个正方形,大小是50*50
*/
// private static final Dimension CHESSGRID_SIZE = new Dimension(1080 / 4 * 3 / 8, 1080 / 4 * 3 / 8);
private static final Color[] BACKGROUND_COLORS = {Color.WHITE, Color.BLACK};
/**
* handle click event
*/
private ClickController clickController;
/**
* chessboardPoint: 表示8*8棋盘中,当前棋子在棋格对应的位置,如(0, 0), (1, 0), (0, 7),(7, 7)等等
* <br>
* chessColor: 表示这个棋子的颜色,有白色,黑色,无色三种
* <br>
* selected: 表示这个棋子是否被选中
*/
private ChessboardPoint chessboardPoint;
protected final ChessColor chessColor;
private boolean selected;
protected ChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor chessColor, ClickController clickController, int size) {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
setLocation(location);
setSize(size, size);
this.chessboardPoint = chessboardPoint;
this.chessColor = chessColor;
this.selected = false;
this.clickController = clickController;
}
public ChessboardPoint getChessboardPoint() {
return chessboardPoint;
}
public void setChessboardPoint(ChessboardPoint chessboardPoint) {
this.chessboardPoint = chessboardPoint;
}
public ChessColor getChessColor() {
return chessColor;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
/**
* @param another 主要用于和另外一个棋子交换位置
* <br>
* 调用时机是在移动棋子的时候,将操控的棋子和对应的空位置棋子(EmptySlotComponent)做交换
*/
public void swapLocation(ChessComponent another) {
ChessboardPoint chessboardPoint1 = getChessboardPoint(), chessboardPoint2 = another.getChessboardPoint();
Point point1 = getLocation(), point2 = another.getLocation();
setChessboardPoint(chessboardPoint2);
setLocation(point2);
another.setChessboardPoint(chessboardPoint1);
another.setLocation(point1);
}
/**
* @param e 响应鼠标监听事件
* <br>
* 当接收到鼠标动作的时候,这个方法就会自动被调用,调用所有监听者的onClick方法,处理棋子的选中,移动等等行为。
*/
@Override
protected void processMouseEvent(MouseEvent e) {
super.processMouseEvent(e);
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
System.out.printf("Click [%d,%d]\n", chessboardPoint.getX(), chessboardPoint.getY());
clickController.onClick(this);
}
}
/**
* @param chessboard 棋盘
* @param destination 目标位置,如(0, 0), (0, 7)等等
* @return this棋子对象的移动规则和当前位置(chessboardPoint)能否到达目标位置
* <br>
* 这个方法主要是检查移动的合法性,如果合法就返回true,反之是false
*/
public abstract boolean canMoveTo(ChessComponent[][] chessboard, ChessboardPoint destination);
/**
* 这个方法主要用于加载一些特定资源,如棋子图片等等。
*
* @throws IOException 如果一些资源找不到(如棋子图片路径错误),就会抛出异常
*/
public abstract void loadResource() throws IOException;
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
System.out.printf("repaint chess [%d,%d]\n", chessboardPoint.getX(), chessboardPoint.getY());
Color squareColor = BACKGROUND_COLORS[(chessboardPoint.getX() + chessboardPoint.getY()) % 2];
g.setColor(squareColor);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}