-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDessertGame.java
More file actions
81 lines (70 loc) · 2.53 KB
/
DessertGame.java
File metadata and controls
81 lines (70 loc) · 2.53 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
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.application.Platform;
import javafx.geometry.Pos;
import java.awt.event.MouseEvent;
import java.util.Random;
public class DessertGame extends Application {
private int score = 0;
@Override
public void start(final Stage stage) {
// Step 3 & 4
BorderPane borderPane = new BorderPane();
Scene scene = new Scene(borderPane, 640, 480);
stage.setTitle("Dessert in the Desert JavaFX Game");
// Step 5
Label scoreLabel = new Label("Score: 0");
borderPane.setTop(scoreLabel);
BorderPane.setAlignment(scoreLabel, Pos.TOP_LEFT);
Button exitButton = new Button("Exit");
exitButton.setOnAction(event -> {
Platform.exit();
});
borderPane.setBottom(exitButton);
BorderPane.setAlignment(exitButton, Pos.BOTTOM_RIGHT);
// Step 6
Pane pane = new Pane();
borderPane.setCenter(pane);
BorderPane.setAlignment(pane, Pos.CENTER);
// TODO: Step 7-10
Button[] buttons = new Button[8];
String dessertButtonInput = "dessertButton";
buttons[0] = new Button(dessertButtonInput);
buttons[0].setOnAction(e -> {
score++;
scoreLabel.setText("Score: " + score);
randomizeButtonPositions(new Random(), buttons);
exitButton.requestFocus();
});
pane.getChildren().add(buttons[0]);
String desertButton = "desertButton";
for(int i = 1; i < 8; i++) {
buttons[i] = new Button(desertButton);
buttons[i].setOnAction(e -> {
score--;
scoreLabel.setText("Score: " + score);
randomizeButtonPositions(new Random(), buttons);
exitButton.requestFocus();
});
pane.getChildren().add(buttons[i]);
}
randomizeButtonPositions(new Random(), buttons);
stage.setScene(scene);
stage.show();
exitButton.requestFocus();
}
public void randomizeButtonPositions(Random random, Button[] buttons) {
for(Button currButton : buttons) {
currButton.setLayoutX(random.nextInt(600));
currButton.setLayoutY(random.nextInt(400));
}
}
public static void main(String[] args) {
Application.launch();
}
}