-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLightDemo.java
More file actions
135 lines (114 loc) · 4.39 KB
/
TrafficLightDemo.java
File metadata and controls
135 lines (114 loc) · 4.39 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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.Timer;
import java.util.TimerTask;
public class TrafficLightDemo extends JFrame implements ActionListener {
private JRadioButton redButton, yellowButton, greenButton;
private JButton aiModeButton;
private LightPanel lightPanel;
private Timer aiTimer;
private boolean aiMode = false;
public TrafficLightDemo() {
setTitle("🚦 Traffic Light Simulator v2.0");
setSize(400, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
lightPanel = new LightPanel();
add(lightPanel, BorderLayout.CENTER);
// Buttons
JPanel buttonPanel = new JPanel(new GridLayout(4, 1));
redButton = new JRadioButton("Red");
yellowButton = new JRadioButton("Yellow");
greenButton = new JRadioButton("Green");
aiModeButton = new JButton("🤖 Toggle AI Mode");
aiModeButton.addActionListener(e -> toggleAIMode());
ButtonGroup group = new ButtonGroup();
group.add(redButton);
group.add(yellowButton);
group.add(greenButton);
redButton.addActionListener(this);
yellowButton.addActionListener(this);
greenButton.addActionListener(this);
buttonPanel.add(redButton);
buttonPanel.add(yellowButton);
buttonPanel.add(greenButton);
buttonPanel.add(aiModeButton);
add(buttonPanel, BorderLayout.SOUTH);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == redButton) {
lightPanel.setLightColor("RED");
} else if (e.getSource() == yellowButton) {
lightPanel.setLightColor("YELLOW");
} else if (e.getSource() == greenButton) {
lightPanel.setLightColor("GREEN");
}
}
private void toggleAIMode() {
aiMode = !aiMode;
if (aiMode) {
aiModeButton.setText("🛑 Stop AI Mode");
aiTimer = new Timer();
aiTimer.scheduleAtFixedRate(new TimerTask() {
String[] states = {"RED", "YELLOW", "GREEN"};
int index = 0;
public void run() {
SwingUtilities.invokeLater(() -> {
lightPanel.setLightColor(states[index]);
index = (index + 1) % states.length;
});
}
}, 0, 2000); // every 2 seconds
} else {
aiTimer.cancel();
aiModeButton.setText("🤖 Toggle AI Mode");
}
}
class LightPanel extends JPanel {
private String currentColor = "";
public void setLightColor(String color) {
currentColor = color;
repaint();
playSound(color);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Background
g.drawImage(new ImageIcon("assets/background.jpg").getImage(), 0, 0, getWidth(), getHeight(), this);
g.setColor(Color.BLACK);
g.fillRect(140, 100, 120, 360);
drawCircle(g, 160, 120, Color.RED, "RED".equals(currentColor));
drawCircle(g, 160, 220, Color.YELLOW, "YELLOW".equals(currentColor));
drawCircle(g, 160, 320, Color.GREEN, "GREEN".equals(currentColor));
}
private void drawCircle(Graphics g, int x, int y, Color color, boolean isOn) {
g.setColor(isOn ? color : color.darker().darker());
g.fillOval(x, y, 80, 80);
}
private void playSound(String color) {
String file = switch (color) {
case "RED" -> "assets/siren.wav"; // Be dramatic 😎
case "YELLOW", "GREEN" -> "assets/beep.wav";
default -> null;
};
if (file != null) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(new File(file));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (Exception ex) {
System.out.println("Sound error: " + ex);
}
}
}
}
public static void main(String[] args) {
new TrafficLightDemo();
}
}