-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu
546 lines (485 loc) · 20.4 KB
/
Menu
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
Lina, Roy, Elliot, Mihai, Iga
Version 6.5
12/12/2018
The class Menu provides the layout of the menu to start each gamemode,
moreover it includes the implementation of different hints and the option to show the graph in a circular shape.
*/
package group2;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Pair;
import java.io.File;
import java.util.Optional;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* Menu class
*/
public class Menu extends Application {
private int inputVertices;
private int inputEdges;
public static Pane root = new Pane();
public static int gamemode;
public static Label currentChrom;
private Scene scene = new Scene(root, 1150, 650);
public static int chromaticNUM; // Chromatic number of the chosen graph
public static MediaPlayer mediaPlayer;
public int counterCC;
private boolean isMute = true;
public boolean isReading;
public int[][] inputMatrix;
private int highVertexIndex;
public int seconds=0;
@Override
public void start(Stage primaryStage) throws Exception {
/**
* Media player implementation
*/
try{
Media sound = new Media(new File("GameMusic.MP3").toURI().toString());
mediaPlayer = new MediaPlayer(sound);
mediaPlayer.setCycleCount(mediaPlayer.INDEFINITE);
}catch(Exception e){};
Image background = new Image(("MenuBackGround.GIF"));
ImageView mv = new ImageView(background);
/**
* Pane for the layout of the game menu
*
*/
Pane pane = new Pane();
pane.setPrefSize(800,500);
Label title = new Label("Crazy Chromatic Number Game");
title.setFont(new Font("Verdana", 40));
title.setTextFill(Color.WHITE);
title.relocate(80,10);
Label gameModes = new Label("Menu Options");
gameModes.setFont(new Font("Verdana", 25));
gameModes.setTextFill(Color.WHITE);
gameModes.relocate(300,100);
Label info = new Label();
info.setFont(new Font("Verdana", 15));
info.setTextFill(Color.WHITE);
info.relocate(480,260);
/**
* Three buttons for the different game modes
*/
Button mode1 = new Button("Mode 1");
mode1.relocate(350,200);
Button mode2 = new Button("Mode 2");
mode2.relocate(350,300);
Button mode3 = new Button("Mode 3");
mode3.relocate(350,400);
/**
* Buttons that gives a summary of each game mode
*/
Button rules1 = new Button("?");
rules1.setShape(new Circle(1.5));
rules1.setMaxSize(3,3);
rules1.relocate(420,200);
Button rules2 = new Button("?");
rules2.setShape(new Circle(1.5));
rules2.setMaxSize(3,3);
rules2.relocate(420,300);
Button rules3 = new Button("?");
rules3.setShape(new Circle(1.5));
rules3.setMaxSize(3,3);
rules3.relocate(420,400);
/**
* GAME MODE 1
* The option to read in a graph from a file
* Button opens a directory, user can upload a file
*/
Button file1 = new Button("Choose your own file");
file1.relocate(450,200);
file1.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Graph File");
File file = fileChooser.showOpenDialog(primaryStage);
String userFile = file.getAbsolutePath();
inputMatrix = ReadToAdj.readCreateMatrix(userFile);
inputVertices = inputMatrix.length;
inputEdges = ReadToAdj.getVertices(inputMatrix);
showGameScreen();
}
}));
/**
* GAME MODE 2
* The option to read in a graph from a file
* Button opens a directory, user can upload a file
*/
Button file2 = new Button("Choose your own file");
file2.relocate(450,300);
file2.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Graph File");
File file = fileChooser.showOpenDialog(primaryStage);
String userFile = file.getAbsolutePath();
inputMatrix = ReadToAdj.readCreateMatrix(userFile);
inputVertices = inputMatrix.length;
inputEdges = ReadToAdj.getVertices(inputMatrix);
showGameScreen();
}
}));
pane.getChildren().addAll(mv,title, gameModes, mode1, mode2, mode3, rules1, rules2, rules3, info, file1, file2);
// Mode 1 EventHandler - opens Pop up that asks for input of number of vertices and edges
mode1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openUserInput(1);
}
});
// Mode 2 EventHandler - opens Pop up that asks for input of number of vertices and edges
mode2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openUserInput(2);
}
});
// Mode 3 EventHandler - generates a random graph and starts game
mode3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
setGameMode(3);
Random ran1 = new Random();
int x = ran1.nextInt(15)+1;
inputVertices = x;
inputEdges = 2*x-1;
showGameScreen();
}
});
/**
* Info about each game mode button - EventHandlers
*/
rules1.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
info.setText("Mode 1\n"+" To the bitter end - \n play until you get a chromatic number! \n Good luck & keep it chromatic!");
}
});
rules2.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
info.setText("Mode 2\n" +
"Best upper bound - \n try to find the best upper bound in \n a fixed time frame! Good luck & keep \n it chromatic! ");
}
});
rules3.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
info.setText("Mode 3\n" +
"Random color \n the vertices in an order given \n by a computer! \nGood luck & keep it chromatic!");
}
});
Scene menu = new Scene(pane, 800, 500, Color.BLACK);
primaryStage.setScene(menu);
primaryStage.setTitle("Chromatic Number Game");
primaryStage.show();
}
/**
* Method for game mode 1 and 2 to collect input for vertices and edges and shows the game screen
* @param modeNumber - number of the the game mode that is going to be played
*/
public void openUserInput(int modeNumber) {
Dialog<Pair<String, String>> input = new Dialog<>();
input.setTitle("Game Mode " + modeNumber);
setGameMode(modeNumber);
input.setHeaderText("Game Settings");
input.setGraphic(new ImageView(("settings.png")));
ButtonType loginButtonType = new ButtonType("START", ButtonData.OK_DONE);// Button of the dialog to start game
input.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL); // Button to close input dialog
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField vertices = new TextField();
vertices.setPromptText("Vertices");
TextField edges = new TextField();
edges.setPromptText("Edges");
Button random = new Button("Random graph");
/**
* Random graph button that puts random numbers into the input text fields of vertices and edges
*/
random.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Random rand = new Random();
int x = rand.nextInt(35)+1; // Random number for the number vertices between 1 and 35
int maximumEdges = (x*2) -1;
int y = rand.nextInt(maximumEdges); // Random number for the number of edges between 0 and 2 times the number of vertices minus 1
vertices.setText(Integer.toString(x));
edges.setText(Integer.toString(y));
}
});
/**
* Layout of the input dialog
*/
grid.add(new Label("Number of vertices:"), 0, 0);
grid.add(vertices, 1, 0);
grid.add(new Label("Number of edges:"), 0, 1);
grid.add(edges, 1, 1);
grid.add(random, 2, 1);
Node loginButton = input.getDialogPane().lookupButton(loginButtonType);
input.getDialogPane().setContent(grid);
input.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(vertices.getText(), edges.getText());
}
return null;
});
Optional<Pair<String, String>> result = input.showAndWait(); // Input saved as a optional pair
/**
* If the start button is pressed, the input is saved in the instance variables for the inputVertices and inputEdges
*/
if (result.isPresent()) {
inputVertices = Integer.parseInt(result.get().getKey());
inputEdges = Integer.parseInt(result.get().getValue());
}
showGameScreen(); // start the game with a graph based on the user input
}
/**
* Sets the current chromatic number to the amout of colors the user used
* @param userChrom - amount of colors used by the user
*/
public static void setCurrentChrom(int userChrom){
root.getChildren().removeAll(currentChrom); // remove previous currentChrom
currentChrom = new Label("Colors used: "+userChrom);
Font chromFont = new Font("Calibri",20);
currentChrom.setFont(chromFont);
currentChrom.setLayoutX(1200);
root.getChildren().addAll(currentChrom); // set label to current number of colors
}
/**
* Method to show the game screen - used in all three game modes
*/
public void showGameScreen() {
root.getChildren().clear(); // clear the previous pane of the game screen
//Resets the done color array - all values -1
for(int i=0;i<51;i++){
MenuItemArray.doneColors[i]=-1;
}
/**
* Layout of the game screen
*/
Image paperBackground = new Image("PaperBackground.JPG");
ImageView rootBackground = new ImageView(paperBackground);
root.getChildren().addAll(rootBackground);
Vertex.setIndex(0);
Stage stage = new Stage();
Font chromFont = new Font("Calibri",30);
currentChrom = new Label("Colors used: 0");
currentChrom.setFont(chromFont);
currentChrom.setLayoutX(950);
root.getChildren().addAll(currentChrom);
if(isReading == false) {
RandomNodes graph = new RandomNodes(inputVertices,inputEdges);
graph.createAdjMatrix();
inputMatrix = graph.getAdjMatrix();
chromaticNUM = ReadGraph.fromScartchChromatic(inputMatrix, inputEdges, inputVertices);
System.out.println(chromaticNUM);
highVertexIndex = ReadGraph.highestDegreeVertex(inputMatrix);
} else{
RandomNodes inputGraph = new RandomNodes(inputVertices,inputEdges);
inputGraph.createVArray(inputMatrix);
chromaticNUM = ReadGraph.fromScartchChromatic(inputMatrix, inputEdges, inputVertices);
System.out.println(chromaticNUM);
highVertexIndex = ReadGraph.highestDegreeVertex(inputMatrix);
}
if(gamemode==3){
VertexArray.vertexArray[0].select();
System.out.println("IS THE CODE COMING HERE?");
}
/**
* Button to mute and unmute the background music/media player
*/
Button mute = new Button();
ImageView muteImg = new ImageView(new Image("MuteImage.PNG"));
mute.setGraphic(muteImg);
ImageView unmuteImg = new ImageView(new Image("UnmuteImage.PNG"));
mute.setLayoutX(1100);
mute.setLayoutY(600);
mute.setOnMouseClicked((new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent e) {
if(isMute) {
mediaPlayer.pause();
mute.setGraphic(unmuteImg);
isMute=false;
}else{
mediaPlayer.play();
mute.setGraphic(muteImg);
isMute=true;
}
}
}
));
/**
* Button to reset the game, uses showGameScreen() to restart the game screen
*/
Button reset = new Button("Reset");
ImageView resetImg = new ImageView(new Image("ResetImage.PNG"));
reset.setGraphic(resetImg);
reset.setOnMouseClicked((new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent e) {
stage.hide();
showGameScreen();
}
}
));
root.getChildren().addAll(reset,mute);
// Adding the vertices to the game screen
for (int i = 0; i < inputMatrix.length; i++) {
root.getChildren().addAll(VertexArray.vertexArray[i]);
}
/**
* Hints for the different game modes and their layouts (buttons)
*/
Button hint = new Button("Chromatic");
Button highestDegree = new Button("Highest degree");
Button highestSatu = new Button("Highest Saturation");
Button circle = new Button("Circle");
circle.setLayoutX(150);
circle.setLayoutY(600);
highestSatu.setLayoutX(600);
highestSatu.setLayoutY(600);
highestDegree.setLayoutX(450);
highestDegree.setLayoutY(600);
hint.setLayoutX(300);
hint.setLayoutY(600);
Button nextColor = new Button("Color next");
nextColor.setLayoutX(750);
nextColor.setLayoutY(600);
/**
* Hint that displays the Chromatic Numberin a alert/pop-up window
*/
hint.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Alert hinta = new Alert(AlertType.INFORMATION);
hinta.setHeaderText("chromatic");
hinta.setContentText("the chromatic number is " + chromaticNUM);
hinta.showAndWait();
}
}));
/**
* Hint that sets the color of the vertex with the highest degree in whi
*/
highestDegree.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Alert hint1 = new Alert(AlertType.INFORMATION);
hint1.setHeaderText("highest degree");
hint1.setContentText("the vertex with the highest degree is " + highVertexIndex);
MenuItemArray.setWhite(highVertexIndex);
hint1.showAndWait();
}
}));
highestSatu.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Alert hint2 = new Alert(AlertType.INFORMATION);
int highestSatIndex = MenuItemArray.highestSaturation(inputMatrix);
hint2.setHeaderText("Highest Saturation");
hint2.setContentText("the vertex with the highest degree of saturation " + highestSatIndex);
MenuItemArray.setSalmon(highestSatIndex);
hint2.showAndWait();
}
}));
nextColor.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Alert hint3 = new Alert(AlertType.INFORMATION);
int highestSatIndex = MenuItemArray.highestSaturation(inputMatrix);
MenuItemArray.colorNext(inputMatrix,highestSatIndex);
hint3.setHeaderText("hopefully this will color the next");
hint3.setContentText("hope this shit works");
hint3.showAndWait();
}
}));
circle.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Alert hintcircle = new Alert(AlertType.INFORMATION);
hintcircle.setHeaderText("circle");
hintcircle.setContentText("vertices form a circle now");
Alert hintrandom = new Alert(AlertType.INFORMATION);
hintrandom.setHeaderText("random");
hintrandom.setContentText("vertices are in radom places now");
if(counterCC%2==0){for (int i=0; i<inputVertices; i++){
VertexArray.vertexArray[i].setLocation(inputVertices);
}counterCC++;
circle.setText("Random");
hintcircle.showAndWait();
}
else {
for (int i=0; i<inputVertices; i++){VertexArray.vertexArray[i].setRLocation(inputVertices);}
counterCC++;
circle.setText("Circle");
hintrandom.showAndWait();
}
}
}));
if(gamemode==2) {
Label timerLabel = new Label("00:00");
seconds = inputVertices * 10;
timerLabel.setLayoutX(600);
timerLabel.setLayoutY(800);
Timer timer = new Timer();
root.getChildren().addAll(hint, highestDegree, highestSatu, nextColor,circle,timerLabel);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
Platform.runLater(() -> timerLabel.setText(Integer.toString(((seconds - (seconds % 60)) / 60)) + ":" + Integer.toString((seconds % 60))));
seconds--;
if(seconds == 0 || MenuItemArray.isFinished(VertexArray.vertexArray,inputVertices) == true){
timer.cancel();
}
}},0,1000);
}else if( gamemode == 1) {
Label timerLabel = new Label("00:00");
timerLabel.setLayoutX(600);
timerLabel.setLayoutY(800);
Timer timer = new Timer();
root.getChildren().addAll(hint, highestDegree, highestSatu, nextColor,circle,timerLabel);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
Platform.runLater(() -> timerLabel.setText(Integer.toString(((seconds - (seconds % 60)) / 60)) + ":" + Integer.toString((seconds % 60))));
seconds++;
}},0,1000);
}
else{
root.getChildren().addAll(hint, highestDegree, highestSatu,circle);
}
stage.setTitle("Chromatic Number Game");
// stage.setOnCloseRequest((new EventHandler<WindowEvent>(){
// @Override
// public void handle(WindowEvent e){
// //Platform.exit();
// mediaPlayer.pause();
// }}));
stage.setScene(scene);
stage.show();
}
// Main method
public static void main(String[] args) {
launch(args);
}
public void setGameMode(int number){
gamemode = number;
}
}