forked from gjy03/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRookChessComponent.java
More file actions
117 lines (104 loc) · 3.81 KB
/
RookChessComponent.java
File metadata and controls
117 lines (104 loc) · 3.81 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
package model;
import view.ChessboardPoint;
import controller.ClickController;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
/**
* 这个类表示国际象棋里面的车
*/
public class RookChessComponent extends ChessComponent {
/**
* 黑车和白车的图片,static使得其可以被所有车对象共享
* <br>
* FIXME: 需要特别注意此处加载的图片是没有背景底色的!!!
*/
private static Image ROOK_WHITE;
private static Image ROOK_BLACK;
/**
* 车棋子对象自身的图片,是上面两种中的一种
*/
private Image rookImage;
/**
* 读取加载车棋子的图片
*
* @throws IOException
*/
public void loadResource() throws IOException {
if (ROOK_WHITE == null) {
ROOK_WHITE = ImageIO.read(new File("./images/rook-white.png"));
}
if (ROOK_BLACK == null) {
ROOK_BLACK = ImageIO.read(new File("./images/rook-black.png"));
}
}
/**
* 在构造棋子对象的时候,调用此方法以根据颜色确定rookImage的图片是哪一种
*
* @param color 棋子颜色
*/
private void initiateRookImage(ChessColor color) {
try {
loadResource();
if (color == ChessColor.WHITE) {
rookImage = ROOK_WHITE;
} else if (color == ChessColor.BLACK) {
rookImage = ROOK_BLACK;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public RookChessComponent(ChessboardPoint chessboardPoint, Point location, ChessColor color, ClickController listener, int size) {
super(chessboardPoint, location, color, listener, size);
initiateRookImage(color);
}
/**
* 车棋子的移动规则
*
* @param chessComponents 棋盘
* @param destination 目标位置,如(0, 0), (0, 7)等等
* @return 车棋子移动的合法性
*/
@Override
public boolean canMoveTo(ChessComponent[][] chessComponents, ChessboardPoint destination) {
ChessboardPoint source = getChessboardPoint();
if (source.getX() == destination.getX()) {
int row = source.getX();
for (int col = Math.min(source.getY(), destination.getY()) + 1;
col < Math.max(source.getY(), destination.getY()); col++) {
if (!(chessComponents[row][col] instanceof EmptySlotComponent)) {
return false;
}
}
} else if (source.getY() == destination.getY()) {
int col = source.getY();
for (int row = Math.min(source.getX(), destination.getX()) + 1;
row < Math.max(source.getX(), destination.getX()); row++) {
if (!(chessComponents[row][col] instanceof EmptySlotComponent)) {
return false;
}
}
} else { // Not on the same row or the same column.
return false;
}
return true;
}
/**
* 注意这个方法,每当窗体受到了形状的变化,或者是通知要进行绘图的时候,就会调用这个方法进行画图。
*
* @param g 可以类比于画笔
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// g.drawImage(rookImage, 0, 0, getWidth() - 13, getHeight() - 20, this);
g.drawImage(rookImage, 0, 0, getWidth() , getHeight(), this);
g.setColor(Color.BLACK);
if (isSelected()) { // Highlights the model if selected.
g.setColor(Color.RED);
g.drawOval(0, 0, getWidth() , getHeight());
}
}
}