-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandPanel.java
More file actions
128 lines (113 loc) · 5.28 KB
/
CommandPanel.java
File metadata and controls
128 lines (113 loc) · 5.28 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
package chess;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CommandPanel extends JPanel {
static CountdownTimer runningTimer;
static boolean paused;
static JPanel playerPanel;
static JLabel whiteLabel, dashLabel, blackLabel;
static JPanel commandPanel;
static JButton offerDraw, resign, pauseOrResume, flipBoard;
public CommandPanel() {
runningTimer = null;
paused = false;
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// Player Panel
playerPanel = new JPanel();
add(playerPanel);
whiteLabel = new JLabel ("White");
whiteLabel.setOpaque(true);
whiteLabel.setBackground(Color.green);
whiteLabel.setFont(new Font("Serif", Font.BOLD, 20));
playerPanel.add(whiteLabel);
dashLabel = new JLabel("-");
dashLabel.setFont(new Font("Serif", Font.BOLD, 20));
playerPanel.add(dashLabel);
blackLabel = new JLabel ("Black");
blackLabel.setOpaque(true);
blackLabel.setFont(new Font("Serif", Font.BOLD, 20));
playerPanel.add(blackLabel);
// Command Panel
commandPanel = new JPanel();
add(commandPanel);
offerDraw = new JButton("Offer Draw");
offerDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String player, opponent;
if (ChessGame.white) {
player = StartScreen.getWhiteName();
opponent = StartScreen.getBlackName();
} else {
player = StartScreen.getBlackName();
opponent = StartScreen.getWhiteName();
}
int answer = JOptionPane.showConfirmDialog(ChessBoard.chessBoard, player + ", do you really want to offer a draw?",
"Select an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
int opponentAnswer = JOptionPane.showConfirmDialog(ChessBoard.chessBoard,
opponent + ", do you accept " + player + "'s draw offer?",
"Select an option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (opponentAnswer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(ChessBoard.chessBoard, "Draw by mutual agreement.\nGame over.");
ChessGame.endGame();
}
}
}
});
commandPanel.add(offerDraw);
resign = new JButton("Resign");
resign.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String player;
if (ChessGame.white) {
player = StartScreen.getWhiteName();
} else {
player = StartScreen.getBlackName();
}
int answer = JOptionPane.showConfirmDialog(ChessBoard.chessBoard, player + ", do you really want to resign?",
"Select an option", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (answer == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(ChessBoard.chessBoard, player + " resigns.\nGame over.");
ChessGame.endGame();
}
}
});
commandPanel.add(resign);
pauseOrResume = new JButton("Pause/Resume");
pauseOrResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!paused) {
if (ChessGame.white && GameRecord.white.timer.isRunning()) {
runningTimer = GameRecord.white.timer;
} else if (ChessGame.white && GameRecord.white.delayTimer.isRunning()) {
runningTimer = GameRecord.white.delayTimer;
} else if (!ChessGame.white && GameRecord.black.timer.isRunning()) {
runningTimer = GameRecord.black.timer;
} else if (!ChessGame.white && GameRecord.black.delayTimer.isRunning()) {
runningTimer = GameRecord.black.delayTimer;
}
if (runningTimer != null) {
runningTimer.stop();
paused = true;
ChessBoard.chessBoard.removeMouseListener(ChessBoard.mouseListener);
ChessBoard.chessBoard.removeMouseMotionListener(ChessBoard.mouseMotionListener);
}
} else {
runningTimer.restart();
paused = false;
ChessBoard.chessBoard.addMouseListener(ChessBoard.mouseListener);
ChessBoard.chessBoard.addMouseMotionListener(ChessBoard.mouseMotionListener);
}
}
});
commandPanel.add(pauseOrResume);
flipBoard = new JButton("Flip Board");
flipBoard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChessBoard.flipBoard();
}
});
commandPanel.add(flipBoard);
}
}