-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationGUI.java
More file actions
89 lines (71 loc) · 2.65 KB
/
Copy pathSimulationGUI.java
File metadata and controls
89 lines (71 loc) · 2.65 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
//This is the GUI simulation
// ChatGPT assisted me to code this GUI
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class SimulationGUI extends JFrame {
private final CampusRoute campusRoute;
private final SimulationController controller;
private final JPanel busPanel;
private final JButton startButton;
private final JButton stopButton;
public SimulationGUI(CampusRoute campusRoute, SimulationController controller) {
this.campusRoute = campusRoute;
this.controller = controller;
setTitle("Campus Bus Simulation");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
busPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawStops(g);
drawBuses(g);
}
};
busPanel.setBackground(Color.WHITE);
add(busPanel, BorderLayout.CENTER);
// Buttons panel
JPanel controlPanel = new JPanel();
startButton = new JButton("STAR");
stopButton = new JButton("STOP");
startButton.addActionListener(e -> {
controller.startSimulation();
startButton.setEnabled(false);
stopButton.setEnabled(true);
});
stopButton.addActionListener(e -> {
controller.stopSimulation();
startButton.setEnabled(true);
stopButton.setEnabled(false);
});
stopButton.setEnabled(false);
controlPanel.add(startButton);
controlPanel.add(stopButton);
add(controlPanel, BorderLayout.SOUTH);
Timer timer = new Timer(33, e -> busPanel.repaint());
timer.start();
}
private void drawStops(Graphics g) {
List<Stop> stops = campusRoute.getStops();
int numStops = stops.size();
g.setColor(Color.BLUE);
for (int i = 0; i < numStops; i++) {
int[] coords = campusRoute.getStopCoordinates(i);
int x = coords[0];
int y = coords[1];
g.fillOval(x - 10, y - 10, 20, 20);
g.drawString("Stop " + i, x - 15, y - 15);
}
}
private void drawBuses(Graphics g) {
g.setColor(Color.RED);
for (Bus bus : campusRoute.getBuses()) {
int x = bus.getCurrentX();
int y = bus.getCurrentY();
g.fillRect(x - 10, y - 5, 20, 10);
g.drawString("Bus " + bus.getBusId(), x - 15, y - 10);
}
}
}