-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckersController.java
More file actions
138 lines (113 loc) · 3.73 KB
/
Copy pathCheckersController.java
File metadata and controls
138 lines (113 loc) · 3.73 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aws52bcheckers;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author DovaReborn
*/
public class CheckersController implements Initializable {
private CheckerBoard board;
private Stage stage;
private Scene scene;
//since rows=cols, just use one variable for both
private int numRowCol;
private double boardWidth;
private double boardHeight;
private Color lightColor;
private Color darkColor;
@FXML
private StackPane stackPane;
@FXML
private MenuBar menuBar;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
board = null;
lightColor = Color.RED;
darkColor = Color.BLACK;
numRowCol = 8;
}
public void ready(Stage stage, Scene scene){
this.stage = stage;
this.scene = scene;
ChangeListener<Number> listener = (ObservableValue<? extends Number> observable, Number oldValue, final Number newValue) -> {
render();
};
stage.widthProperty().addListener(listener);
stage.heightProperty().addListener(listener);
render();
}
public void render(){
stackPane.getChildren().clear();
double margin;
boardWidth = scene.getWidth();
boardHeight = scene.getHeight() - menuBar.getHeight();//allow for menu bar
board = new CheckerBoard(numRowCol, numRowCol, boardWidth, boardHeight, lightColor, darkColor);
if(boardHeight>boardWidth){
margin = (boardHeight-boardWidth)/2;
StackPane.setMargin(board.getBoard(), new Insets(margin, 0, margin, 0));
}else{
margin = (boardWidth-boardHeight)/2;
StackPane.setMargin(board.getBoard(), new Insets(0, margin, 0, margin));
}
stackPane.getChildren().add(board.getBoard());
}
@FXML
public void changeNumBoxes(ActionEvent event){
MenuItem source = (MenuItem)event.getSource();
switch(source.getId()){
case "sixteen":
numRowCol = 16;
break;
case "ten":
numRowCol = 10;
break;
/*case "eight"://just let this case default to the end
numRowCol = 8;
break;*/
case "three":
numRowCol = 3;
break;
default:
numRowCol = 8;
break;
}
render();
}
@FXML
public void changeColor(ActionEvent event){
MenuItem source = (MenuItem)event.getSource();
switch(source.getId()){
case "blue":
lightColor = Color.SKYBLUE;
darkColor = Color.DARKBLUE;
break;
default:
lightColor = Color.RED;
darkColor = Color.BLACK;
break;
}
render();
}
}