-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulationApp.java
172 lines (140 loc) · 6.18 KB
/
SimulationApp.java
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
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class SimulationApp extends Application {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private Environment environment;
private AnimationTimer timer;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Ecosystem Simulation");
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
environment = new Environment();
// Sidebar with controls
VBox controls = new VBox(10);
controls.setStyle("-fx-padding: 10;");
Label speedLabel = new Label("Simulation Speed:");
Slider speedSlider = new Slider(1, 10, 5);
Label sizeLabel = new Label("Organism Size:");
Slider sizeSlider = new Slider(5, 50, 20);
Label speedOrgLabel = new Label("Organism Speed:");
Slider speedOrgSlider = new Slider(0.5, 5.0, 2.0);
Label senseLabel = new Label("Sense Radius:");
Slider senseSlider = new Slider(50, 200, 100);
Button addOrgButton = new Button("Add Organism");
Button addFoodButton = new Button("Add Food");
Button startButton = new Button("Start Simulation");
Button stopButton = new Button("Stop Simulation");
controls.getChildren().addAll(
speedLabel, speedSlider,
sizeLabel, sizeSlider,
speedOrgLabel, speedOrgSlider,
senseLabel, senseSlider,
addOrgButton, addFoodButton,
startButton, stopButton
);
// Set up the root layout
BorderPane root = new BorderPane();
root.setCenter(canvas);
root.setRight(controls);
Scene scene = new Scene(root, WIDTH + 200, HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
// Add initial organisms to the environment
environment.addOrganism(new Organism(
20, 2, 100.0,
environment.getSafeZoneX(), environment.getSafeZoneY(),
environment.getSafeZoneWidth(), environment.getSafeZoneHeight()
));
environment.addOrganism(new Organism(
30, 1.5, 80.0,
environment.getSafeZoneX(), environment.getSafeZoneY(),
environment.getSafeZoneWidth(), environment.getSafeZoneHeight()
));
// Add initial food sources
environment.addFood(new Food(100, 100));
environment.addFood(new Food(200, 150));
environment.addFood(new Food(400, 300));
// Animation loop
timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.clearRect(0, 0, WIDTH, HEIGHT);
// Draw the safe zone
gc.setFill(Color.LIGHTGRAY);
gc.fillRect(environment.getSafeZoneX(), environment.getSafeZoneY(), environment.getSafeZoneWidth(), environment.getSafeZoneHeight());
// Draw food
gc.setFill(Color.GREEN);
for (Food food : environment.getFoodSources()) {
gc.fillOval(food.getX(), food.getY(), 5, 5);
}
// Draw organisms
gc.setFill(Color.BLUE);
for (Organism organism : environment.getOrganisms()) {
gc.fillOval(organism.getX(), organism.getY(), organism.getSize(), organism.getSize());
}
environment.simulateStep();
}
};
// Start button action
startButton.setOnAction(e -> timer.start());
// Stop button action
stopButton.setOnAction(e -> timer.stop());
// Add organism button action
addOrgButton.setOnAction(e -> {
double size = sizeSlider.getValue();
double speed = speedOrgSlider.getValue();
double senseRadius = senseSlider.getValue();
environment.addOrganism(new Organism(
size, speed, senseRadius,
environment.getSafeZoneX(), environment.getSafeZoneY(),
environment.getSafeZoneWidth(), environment.getSafeZoneHeight()
));
});
// Add food button action
addFoodButton.setOnAction(e -> {
double x = Math.random() * WIDTH;
double y = Math.random() * HEIGHT;
environment.addFood(new Food(x, y));
});
// Adjust simulation speed based on slider
speedSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
timer.stop();
timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.clearRect(0, 0, WIDTH, HEIGHT);
// Draw the safe zone
gc.setFill(Color.LIGHTGRAY);
gc.fillRect(environment.getSafeZoneX(), environment.getSafeZoneY(), environment.getSafeZoneWidth(), environment.getSafeZoneHeight());
// Draw food
gc.setFill(Color.GREEN);
for (Food food : environment.getFoodSources()) {
gc.fillOval(food.getX(), food.getY(), 5, 5);
}
// Draw organisms
gc.setFill(Color.BLUE);
for (Organism organism : environment.getOrganisms()) {
gc.fillOval(organism.getX(), organism.getY(), organism.getSize(), organism.getSize());
}
environment.simulateStep();
}
};
timer.start();
});
}
public static void main(String[] args) {
launch(args);
}
}