-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyController.java
More file actions
87 lines (75 loc) · 2.45 KB
/
MyController.java
File metadata and controls
87 lines (75 loc) · 2.45 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
package mines;
import java.io.File;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
//MyController is a controller class to access and use the properties of the scene built by the SceneBuilder into FXML file.
public class MyController {
// properties from the FXML file:
@FXML
private Label minesC;
@FXML
private TextField heightVal;
@FXML
private TextField minesVal;
@FXML
private TextField widthVal;
@FXML
private HBox lifes;
// additional properties:
private MinesFX mainClass;
@FXML
// reset is a method created from the FXML file to define what the 'reset' button does.
// when the 'reset' button clicked, it creates a new blank game board.
void reset(ActionEvent event) {
mainClass.createBoardGrid(getHeight(), getWidth(), getMines(), true);
}
@FXML
// startOver method, resets the same game board for another try when the 'StartOver' button clicked.
void StartOver(ActionEvent event) {
mainClass.startOver();
}
// setLifes method, gets the number of lifes and sets the hearts images in their correct way.
void setLifes(int num) {
int i = 0;
ObservableList<Node> hearts = lifes.getChildren();
for (Node tmp : hearts) {
ImageView temp = (ImageView) tmp;
if (i < num) {
File file = new File("src/mines/redHeart.PNG");
temp.setImage(new Image(file.toURI().toString()));
} else {
File file = new File("src/mines/greyHeart.PNG");
temp.setImage(new Image(file.toURI().toString()));
}
i++;
}
}
// setMainClass is a helping method to make the main class be recognizable by the controller, so it can use it's methods.
public void setMainClass(MinesFX mainClass) {
this.mainClass = mainClass;
}
// getWidth method, returns the widthVal field.
public int getWidth() {
return Integer.parseInt(widthVal.getText());
}
// getHeight method, returns the heightVal field.
public int getHeight() {
int temp = Integer.parseInt(heightVal.getText());
return temp;
}
// getMines method, returns the minesVal field.
public int getMines() {
return Integer.parseInt(minesVal.getText());
}
// setNewMinesCounting changes the number in the label minesCounting.
public void setNewMinesCounting(int count, int mines) {
minesC.setText("mines : " + count + " / " + mines);
}
}