-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessUI.java
More file actions
213 lines (167 loc) · 6.9 KB
/
ChessUI.java
File metadata and controls
213 lines (167 loc) · 6.9 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessUI extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
int cellSize;
private ImageIcon[] chessIcons;
private JLabel[] pieceLabels;
JLabel movingChessPiece;
int originXMove;
int originYMove;
int destXMove;
int destYMove;
boolean moveFreshness;
int xAdjustment;
int yAdjustment;
public ChessUI(){
this.cellSize = 60;
Dimension boardSize = new Dimension(8 * cellSize, 8 * cellSize);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout( new GridLayout(8, 8) );
chessBoard.setPreferredSize( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel( new BorderLayout() );
chessBoard.add( square );
square.setBackground( (i + i / 8) % 2 == 1 ? Color.GRAY : Color.WHITE );
}
initIcons();
initPieceLabels();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setResizable(true);
setLocationRelativeTo( null );
setVisible(true);
}
/** ---------------------------------------------------------------------------------
* Chess piece graphics **/
private void initIcons() {
chessIcons = new ImageIcon[] {
new ImageIcon("./img/Chess_kdt60.png"),
new ImageIcon("./img/Chess_qdt60.png"),
new ImageIcon("./img/Chess_rdt60.png"),
new ImageIcon("./img/Chess_bdt60.png"),
new ImageIcon("./img/Chess_ndt60.png"),
new ImageIcon("./img/Chess_pdt60.png"),
new ImageIcon("./img/Chess_klt60.png"),
new ImageIcon("./img/Chess_qlt60.png"),
new ImageIcon("./img/Chess_rlt60.png"),
new ImageIcon("./img/Chess_blt60.png"),
new ImageIcon("./img/Chess_nlt60.png"),
new ImageIcon("./img/Chess_plt60.png")
};
}
private void initPieceLabels() {
pieceLabels = new JLabel[64];
for(int k = 0; k < 64; k++) {
JLabel label = new JLabel();
label.setVisible(false);
label.setSize(cellSize, cellSize);
pieceLabels[k] = label;
JPanel panel = (JPanel)chessBoard.getComponent(k);
panel.add(label);
}
}
private JLabel getPieceLabel(int row, int column) {
return pieceLabels[row * 8 + column];
}
private ImageIcon getImageIcon(Piece.Type type, ChessColor color) {
return chessIcons[type.ordinal() + (color == ChessColor.WHITE ? Piece.Type.values().length : 0)];
}
/**
* This method takes a piece of a color and places it in the specified coordinates.
* @param type Desired type
* @param color Desired color (should only be either Color.white or Color.black)
* @param coord Coordinates of the piece
*/
public void placePiece(Piece.Type type, ChessColor color, Coordinates coord) {
getPieceLabel(coord.getY(), coord.getX()).setIcon(getImageIcon(type, color));
getPieceLabel(coord.getY(), coord.getX()).setVisible(true);
}
/**
* This method removes the piece at the specified position, if there is any.
* @param coord Coordinates of the piece
*/
public void removePiece(Coordinates coord) {
getPieceLabel(coord.getY(), coord.getX()).setVisible(false);
}
/** ---------------------------------------------------------------------------------
* Events management **/
/**
* This method waits for the player to enter a move in the UI. Calling this is blocking.
*/
public FromTo waitForPlayerMove() {
moveFreshness = false;
while(! moveFreshness)
try {
synchronized (this.getClass()) {
this.getClass().wait();
}
}
catch (Exception e) {
System.err.println("ERROR : interrupted while waiting for chess move");
System.err.println(e);
System.exit(-1);
}
return new FromTo(originXMove, originYMove, destXMove, destYMove);
}
public void mousePressed(MouseEvent e){
movingChessPiece = null;
originXMove = e.getX() / cellSize;
originYMove = e.getY() / cellSize;
xAdjustment = - e.getX() + originXMove * cellSize;
yAdjustment = - e.getY() + originYMove * cellSize;
movingChessPiece = new JLabel(getPieceLabel(originYMove, originXMove).getIcon());
movingChessPiece.setVisible(getPieceLabel(originYMove, originXMove).isVisible());
movingChessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
if(movingChessPiece.getIcon() != null)
movingChessPiece.setSize(movingChessPiece.getIcon().getIconWidth(), movingChessPiece.getIcon().getIconHeight());
layeredPane.add(movingChessPiece, JLayeredPane.DRAG_LAYER);
}
//Move the chess piece around
public void mouseDragged(MouseEvent me) {
if (movingChessPiece == null) return;
movingChessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
//Drop the chess piece back onto the chess board
public void mouseReleased(MouseEvent e) {
if(movingChessPiece == null) return;
movingChessPiece.setVisible(false);
layeredPane.remove(movingChessPiece);
movingChessPiece = null;
destXMove = e.getX() / cellSize;
destYMove = e.getY() / cellSize;
moveFreshness = true;
synchronized (this.getClass()) {
this.getClass().notifyAll();
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
ChessUI ui = new ChessUI();
ui.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
for(int k = 0; k < Piece.Type.values().length; k++)
ui.placePiece(Piece.Type.values()[k], ChessColor.BLACK, new Coordinates(k, 0));
for(int k = 0; k < Piece.Type.values().length; k++)
ui.placePiece(Piece.Type.values()[k], ChessColor.WHITE, new Coordinates(k, 1));
ui.pack();
ui.setResizable(true);
ui.setLocationRelativeTo( null );
ui.setVisible(true);
}
}