-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovableCircle.java
More file actions
67 lines (55 loc) · 2.15 KB
/
MovableCircle.java
File metadata and controls
67 lines (55 loc) · 2.15 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
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MovableCircle extends Application {
private double circleRadius = 50;
private double circleCenterX = 300;
private double circleCenterY = 200;
private double moveBy = 10;
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(circleCenterX, circleCenterY, circleRadius);
circle.setStroke(Color.BLACK);
circle.setFill(Color.TRANSPARENT);
Button upButton = new Button("Up");
upButton.setOnAction(e -> {
if (circle.getCenterY() - circleRadius - moveBy > 0) {
circle.setCenterY(circle.getCenterY() - moveBy);
}
});
Button downButton = new Button("Down");
downButton.setOnAction(e -> {
if (circle.getCenterY() + circleRadius + moveBy < 400) { // Assuming 400 is the scene height
circle.setCenterY(circle.getCenterY() + moveBy);
}
});
Button leftButton = new Button("Left");
leftButton.setOnAction(e -> {
if (circle.getCenterX() - circleRadius - moveBy > 0) {
circle.setCenterX(circle.getCenterX() - moveBy);
}
});
Button rightButton = new Button("Right");
rightButton.setOnAction(e -> {
if (circle.getCenterX() + circleRadius + moveBy < 600) { // Assuming 600 is the scene width
circle.setCenterX(circle.getCenterX() + moveBy);
}
});
VBox buttons = new VBox(10, upButton, downButton, leftButton, rightButton);
Pane root = new Pane();
root.getChildren().addAll(circle, buttons);
buttons.relocate(10, 350);
Scene scene = new Scene(root, 600, 400);
primaryStage.setTitle("Movable Circle with Boundary");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}