-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChessApplication.java
More file actions
237 lines (198 loc) · 7.41 KB
/
ChessApplication.java
File metadata and controls
237 lines (198 loc) · 7.41 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//Chess application -- Thomas Hood (2847586) & Julie-Anne Glennon (2864184) -- BScH Yr 3
//imports
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.PrintStream;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.util.Duration;
//class definition
public class ChessApplication extends Application {
// private fields for this class
private StackPane sp_mainlayout; //layout which allows items to be positioned on top of each other
private CustomControl cc_custom; //control which has a board and detects mouse and keyboard events
private Button btn1, btn2, btn3, btn4;
private Timeline timeline;
private Timeline timeline2;
private int type;
private TextArea status;
private static Label timerwhite = new Label();
private static Label timerblack = new Label();
private SimpleDoubleProperty TimeSeconds = new SimpleDoubleProperty();
private SimpleDoubleProperty timeSeconds = new SimpleDoubleProperty();
private Duration time1 = Duration.minutes(15), Time2 = Duration.seconds(900);
// private PrintStream ps;
// overridden init method
@Override
public void init() {
// initialize the layout, create a CustomControl and it to the layout
sp_mainlayout = new StackPane();
cc_custom = new CustomControl();
sp_mainlayout.getChildren().add(cc_custom);
// Configure the Labels
// Bind the timerLabel text property to the timeSeconds property
timerwhite.setText("15");
timerblack.setText("15");
timeSeconds.set(Duration.minutes(15).toMinutes());
TimeSeconds.set(Duration.minutes(15).toMinutes());
timerwhite.textProperty().bind(timeSeconds.asString("%.2f"));
timerwhite.setTextFill(Color.WHITE);
timerwhite.setStyle("-fx-font-size: 2em;");
timerblack.textProperty().bind(TimeSeconds.asString("%.2f"));
timerblack.setTextFill(Color.BLACK);
timerblack.setStyle("-fx-font-size: 2em;");
btn1 = new Button("START");
btn2 = new Button("STOP");
btn3 = new Button("START");
btn4 = new Button("STOP");
btn1.setOnAction(new EventHandler<ActionEvent>() {
//overridden handle method
@Override
public void handle(ActionEvent event) {
if (timeline != null && type == 1){
timeSeconds.set(Duration.minutes(15).toMinutes());
}
else {
timeline = new Timeline(
new KeyFrame(Duration.millis(1),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Duration duration = ((KeyFrame)t.getSource()).getTime();
if(Time2.greaterThan(Duration.ZERO)){
time1 = time1.subtract(duration);}
else{time1 = Duration.ZERO;}
timeSeconds.set(time1.toMinutes());
}
})
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
//event.consume();
}
}
});
btn2.setOnAction(new EventHandler<ActionEvent>() {
//overridden handle method
@Override
public void handle(ActionEvent event) {
timeline.stop();
//event.consume();
}
});
btn3.setOnAction(new EventHandler<ActionEvent>() {
//overridden handle method
public void handle(ActionEvent event) {
if (timeline2 != null && type == 2){
TimeSeconds.set(Duration.minutes(15).toMinutes());
// TimeSeconds.set(Time2.toSeconds());
}
else {
timeline2 = new Timeline(
new KeyFrame(Duration.millis(1),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Duration duration = ((KeyFrame)t.getSource()).getTime();
if(Time2.greaterThan(Duration.ZERO)){
Time2 = Time2.subtract(duration);}
else{Time2 = Duration.ZERO;}
TimeSeconds.set(Time2.toMinutes());
}
})
);
timeline2.setCycleCount(Timeline.INDEFINITE);
timeline2.play();
//event.consume();
}
}
});
btn4.setOnAction(new EventHandler<ActionEvent>() {
//overridden handle method
@Override
public void handle(ActionEvent event) {
timeline2.stop();
//event.consume();
}
});
}
// overridden start method
@Override
public void start(Stage primaryStage) {
// set the title and scene, and show the stage
primaryStage.setTitle("Chess");
primaryStage.setScene(new Scene(sp_mainlayout, 1000, 800));
// create text box
TextArea status = new TextArea();
status.setEditable(false);
status.setPromptText("Status Updates");
status.setStyle("-fx-font-size: 12;");
status.setPrefWidth(200);
status.setPrefHeight(800);
PrintStream ps = System.out;
System.setOut(new TextAreaPrintStream(status, ps));
VBox vb = new VBox();
vb.getChildren().add(status);
vb.setPrefWidth(200);
BorderPane bp = new BorderPane();
HBox hb1 = new HBox();
HBox hb2 = new HBox();
hb1.setAlignment(Pos.CENTER);
hb1.getChildren().addAll(btn3,timerblack,btn4);
hb1.setSpacing(50);
hb1.setStyle("-fx-background-color: #D3D3D3");
hb1.setLayoutY(30);
hb2.setAlignment(Pos.CENTER);
hb2.getChildren().addAll(btn1,timerwhite,btn2);
hb2.setSpacing(50);
hb2.setStyle("-fx-background-color: #D3D3D3");
hb2.setLayoutY(30);
primaryStage.setScene(new Scene(bp, 1000, 800 ));
bp.setCenter(sp_mainlayout);
bp.setTop(hb1);
bp.setBottom(hb2);
//bp.setLeft(btn1);
bp.setRight(vb);
primaryStage.setMaxHeight(800);
primaryStage.setMaxWidth(1000);
primaryStage.setMinWidth(700);
primaryStage.setMinHeight(500);
primaryStage.show();
}
// overridden stop method
@Override
public void stop(){}
public void appendToStatus(String Text) {
status.appendText(Text);
}
public String getStatusText() {
return status.getText();
}
// Returns timer for white
public static double getTimerWhite(){
double wt = Double.parseDouble(timerwhite.getText());
return wt;
}
// Returns timer for black
public static double getTimerBlack(){
double bt = Double.parseDouble(timerblack.getText());
return bt;
}
// entry point into our program to launch our JavaFX application
public static void main(String[] args) {
launch(args);
}
}