-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
222 lines (187 loc) · 6.77 KB
/
App.java
File metadata and controls
222 lines (187 loc) · 6.77 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import java.util.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.lang.Thread;
public class App extends Thread implements ActionListener {
JFrame frame;
JButton start, stop, addhtv, addltv, addbike, l1, l2, l3, red, green;
JLabel highwayoutput, highwaydensity;
Container c;
Road road;
int running;
int lane;
Graphics g;
int density = 0;
int CAR_COUNT;
long starttime = 0;
public App() {
frame = new JFrame("Traffic Simulation");
frame.setBounds(-10, 0, 1600, 750);
frame.setResizable(false);
// frame.setLayout(road.BorderLayout.CENTER);
road = new Road(0,0,120,1370);
// frame.setLayout(new BorderLayout());
road.setPreferredSize(new Dimension(1600, 500));
frame.add(road, BorderLayout.CENTER);
JPanel controlPanel = new JPanel();
controlPanel.setLayout(null);
controlPanel.setPreferredSize(new Dimension(1600, 250));
highwayoutput = new JLabel("Output: " + 0);
highwayoutput.setBounds(1130, 10, 200, 70);
controlPanel.add(highwayoutput);
highwaydensity = new JLabel("Density: " + 0);
highwaydensity.setBounds(1130, 90, 200, 70);
controlPanel.add(highwaydensity);
start = new JButton("Start");
start.setBounds(0, 10, 150, 70);
start.addActionListener(this);
controlPanel.add(start);
stop = new JButton("Stop");
stop.setBounds(0, 90, 150, 70);
stop.addActionListener(this);
controlPanel.add(stop);
addhtv = new JButton("Add HTV");
addhtv.setBounds(170, 10, 200, 70);
addhtv.addActionListener(this);
controlPanel.add(addhtv);
addltv = new JButton("Add LTV");
addltv.setBounds(170, 90, 200, 70);
addltv.addActionListener(this);
controlPanel.add(addltv);
// addbike = new JButton("Add RB 19");
addbike = new JButton("Add Bike");
addbike.setBounds(170, 170, 200, 70);
addbike.addActionListener(this);
controlPanel.add(addbike);
l1 = new JButton("Lane 1");
l1.setBounds(390, 10, 200, 70);
l1.addActionListener(this);
controlPanel.add(l1);
l2 = new JButton("Lane 2");
l2.setBounds(390, 90, 200, 70);
l2.addActionListener(this);
controlPanel.add(l2);
l3 = new JButton("Lane 3");
l3.setBounds(390, 170, 200, 70);
l3.addActionListener(this);
controlPanel.add(l3);
red = new JButton("RED");
red.setBounds(600, 10, 100, 50);
red.addActionListener(this);
controlPanel.add(red);
green = new JButton("Green");
green.setBounds(600, 70, 100, 50);
green.addActionListener(this);
controlPanel.add(green);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == start) {
if (running == 0) {
running = 1;
Thread t = new Thread(this);
t.start();
road.setCount();
starttime = System.currentTimeMillis();
}
} else if (e.getSource() == stop) {
running = 0;
road.setCount();
} else if (e.getSource() == l1) {
lane = 1;
} else if (e.getSource() == l2) {
lane = 2;
} else if (e.getSource() == l3) {
lane = 3;
} else if (e.getSource() == addhtv) {
if (lane == 1) {
HTV tsthtv = new HTV(10, 15, 1);
road.addveh(tsthtv);
density++;
} else if (lane == 2) {
HTV tsthtv = new HTV(10, 135, 2);
road.addveh(tsthtv);
density++;
} else if (lane == 3) {
HTV tsthtv = new HTV(10, 255, 3);
road.addveh(tsthtv);
density++;
}
} else if (e.getSource() == addltv) {
if (lane == 1) {
LTV tstltv = new LTV(10, 30, 1);
road.addveh(tstltv);
density++;
} else if (lane == 2) {
LTV tstltv = new LTV(10, 150, 2);
road.addveh(tstltv);
density++;
} else if (lane == 3) {
LTV tstltv = new LTV(10, 270, 3);
road.addveh(tstltv);
density++;
}
} else if (e.getSource() == addbike) {
// playSound("Max.wav");
if (lane == 1) {
Bike tstbike = new Bike(10, 50, 1);
road.addveh(tstbike);
density++;
} else if (lane == 2) {
Bike tstbike = new Bike(10, 170, 2);
road.addveh(tstbike);
density++;
} else if (lane == 3) {
Bike tstbike = new Bike(10, 290, 3);
road.addveh(tstbike);
density++;
}
} else if (e.getSource() == red) {
road.trflight = "red";
} else if (e.getSource() == green) {
road.trflight = "green";
}
frame.repaint();
}
private void playSound(String soundFileName) {
try {
File soundFile = new File(soundFileName);
if (soundFile.exists()) {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} else {
System.err.println("Sound file not found: " + soundFileName);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
while (running == 1) {
road.step();
CAR_COUNT = road.getCount();
double tp = CAR_COUNT / (double) (System.currentTimeMillis() - starttime);
highwayoutput.setText("Output: " + tp);
highwaydensity.setText("Density: " + density);
frame.repaint();
try {
Thread.sleep(10);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
App obj = new App();
}
}