-
Notifications
You must be signed in to change notification settings - Fork 10
/
ChessGUI.java
292 lines (254 loc) · 9.47 KB
/
ChessGUI.java
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.util.Optional;
import Networking.Client;
import Networking.NetworkConnection;
import Networking.Server;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChessGUI extends Application
{
public static void main(String[] args)
{
// Automatic VM reset, thanks to Joseph Rachmuth.
try
{
launch(args);
System.exit(0);
}
catch (Exception error)
{
error.printStackTrace();
System.exit(0);
}
}
// Hack: Set connection as public static so it may be used
// by ChessBoard to send moves. (VERY HACKY!)
public static NetworkConnection connection;
private ChessBoard board;
private TextArea chatArea; // chat messages
private boolean playerIsWhite; // white player = server
private boolean offlineMode = false;
@Override
public void start(Stage mainStage)
{
mainStage.setTitle("Chess Game");
mainStage.getIcons().add( new Image("assets/icons/app_icon.png") );
BorderPane root = new BorderPane();
Scene mainScene = new Scene(root);
mainStage.setScene(mainScene);
// add stylesheet
mainScene.getStylesheets().add("assets/stylesheet.css");
// prompt user to select team color
choosePlayerColor();
// draw chessboard
board = new ChessBoard(playerIsWhite);
root.setCenter(board); // sized 400x400
// Initialize server/client
if (!offlineMode)
{
// create chat box
VBox chatBox = generateChatBox();
root.setRight(chatBox);
if (playerIsWhite)
{
connection = createServer();
chatArea.appendText("Connecting to client...\n");
}
else
{
connection = createClient();
chatArea.appendText("Connecting to server...\n");
// lock board until white makes first move
board.setDisable(true);
}
try
{
connection.startConnection();
}
catch ( Exception e )
{
System.err.println("Error: Failed to start connection");
System.exit(1);
}
}
// add menuBar
MenuBar menuBar = generateMenuBar();
root.setTop(menuBar);
mainStage.show();
}
// Attempt to close socket on program termination
// TODO: Check if connection has been created/established
// before attmepting to close
@Override
public void stop() {
try {
connection.closeConnection();
}
catch (NullPointerException e) {
// Nothing to close. Connention never initialized and/or established
}
catch (Exception e) {
e.printStackTrace();
}
}
// Prompts the player to choose team color
// TODO: Change return type to enum so we can return NULL
// if user exits without selecting a color;
public void choosePlayerColor()
{
// TODO: If a chess game is currently ongoing, warn that
// "Starting a new game while a match is in progress will count as a forfiet."
// "Do you still want to start a new game?"
// "Yes" "No"
//
// If no, just break/return and ignore the following code
// Prompt user for new game
Alert newGameAlert = new Alert(AlertType.CONFIRMATION);
newGameAlert.setTitle("Start new game");
newGameAlert.setHeaderText(null);
newGameAlert.setContentText("Pick team color");
ButtonType buttonTypeWhite = new ButtonType("White (Server)");
ButtonType buttonTypeBlack = new ButtonType("Black (Client)");
ButtonType buttonTypeOffline = new ButtonType("Offline");
newGameAlert.getButtonTypes().setAll(buttonTypeWhite, buttonTypeBlack, buttonTypeOffline);
Optional<ButtonType> result = newGameAlert.showAndWait();
if (result.get() == buttonTypeWhite)
{
this.playerIsWhite = true;
}
else if (result.get() == buttonTypeBlack)
{
this.playerIsWhite = false;
}
else // offline mode
{
this.playerIsWhite = true;
this.offlineMode = true;
}
}
// Initialize Server
private Server createServer() {
return new Server(444, data -> {
// Below: Runs whenever data is revieved from the client.
// runLater() gives JavaFX time to draw GUI.
Platform.runLater(() -> {
if (data instanceof MoveInfo)
{
board.processOpponentMove((MoveInfo) data);
}
else // if (data instanceof String)
{
// Display in chat message box
chatArea.appendText(data.toString() + "\n");
}
});
});
}
// Initialize Client
private Client createClient() {
// localhost IP address
return new Client("127.0.0.1", 444, data -> {
// Below: Runs whenever data is revieved from the server.
// runLater() gives JavaFX time to draw GUI.
Platform.runLater(() -> {
if (data instanceof MoveInfo)
{
board.processOpponentMove((MoveInfo) data);
}
else // if (data instanceof String)
{
// Display in chat message box
chatArea.appendText(data.toString() + "\n");
}
});
});
}
// Quits program
public void onQuit()
{
Platform.exit();
System.exit(0);
}
// Display 'about' menu
public void onDisplayAbout()
{
Alert infoAlert = new Alert(AlertType.INFORMATION);
infoAlert.setTitle("About this program");
infoAlert.setHeaderText(null);
// set window icon
Stage alertStage = (Stage) infoAlert.getDialogPane().getScene().getWindow();
alertStage.getIcons().add( new Image("assets/icons/about.png") );
// the graphic replaces the standard icon on the left
//infoAlert.setGraphic( new ImageView( new Image("assets/icons/cat.png", 64, 64, true, true) ) );
infoAlert.setContentText("Programmed by Maxwell Sirotin and Steven Vascellaro.\n\n" +
"Chess icons by \"Colin M.L. Burnett\".\n\n" +
"Networking package & chat client based on \n\"JavaFX Software: Chat (Server-Client)\" \nby Almas Baimagambetov.\n\n" +
"App icon by BlackVariant.");
infoAlert.showAndWait();
}
// Generate chat window
private VBox generateChatBox()
{
// sends messages
TextField chatField = new TextField();
chatField.getStyleClass().add("chat-field");
chatField.setOnAction(event -> {
// Specify if message is from server or client
String message = playerIsWhite ? "Server: " : "Client: ";
message += chatField.getText();
chatField.clear();
chatArea.appendText(message + "\n");
try {
connection.send(message);
}
catch (Exception e) {
chatArea.appendText("Failed to send\n");
}
});
// displays messages
chatArea = new TextArea();
chatArea.setEditable(false);
chatArea.getStyleClass().add("chat-area");
VBox chatBox = new VBox(20, chatArea, chatField);
chatBox.getStyleClass().add("chat-box");
return chatBox;
}
// Generate main menu bar
private MenuBar generateMenuBar()
{
MenuBar menuBar = new MenuBar();
Menu gameMenu = new Menu("Game");
menuBar.getMenus().add(gameMenu);
MenuItem menuItemQuit = new MenuItem("Quit");
menuItemQuit.setOnAction(e -> onQuit());
//menuItemQuit.setGraphic( new ImageView( new Image("assets/icons/quit.png", 16, 16, true, true) ) );
menuItemQuit.setAccelerator( new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_DOWN) );
gameMenu.getItems().add(menuItemQuit);
Menu menuHelp = new Menu("Help");
menuBar.getMenus().add(menuHelp);
MenuItem menuItemAbout = new MenuItem("About");
//menuItemAbout.setGraphic( new ImageView( new Image("assets/icons/about.png", 16, 16, true, true) ) );
// Note: Accelerator F1 does not work if TextField is
// in focus. This is a known issue in JavaFX.
// https://bugs.openjdk.java.net/browse/JDK-8148857
menuItemAbout.setAccelerator( new KeyCodeCombination(KeyCode.F1) );
menuItemAbout.setOnAction(e -> onDisplayAbout());
menuHelp.getItems().add(menuItemAbout);
return menuBar;
}
}