1111
1212import javax .swing .Icon ;
1313import javax .swing .JButton ;
14+ import javax .swing .JCheckBoxMenuItem ;
1415import javax .swing .JFrame ;
1516import javax .swing .JLabel ;
1617import javax .swing .JMenu ;
1718import javax .swing .JMenuBar ;
1819import javax .swing .JMenuItem ;
20+ import javax .swing .JOptionPane ;
1921import javax .swing .JPanel ;
2022import javax .swing .JToolBar ;
2123import javax .swing .border .EmptyBorder ;
2224
2325import Car .Car ;
26+ import FileIO .MapIO ;
2427import Map .Map ;
2528import Map .RandomMapGenerator ;
26- import Map .Tile ;
2729import Map .NonRoad .NonRoad ;
2830import Map .Road .Intersection ;
2931import Map .Road .Road ;
@@ -39,12 +41,17 @@ public class Runner extends JFrame{
3941 static int cars =100 ;//number of cars
4042 static int sizeBox =14 ;//size of each box
4143 static Map map ;
42- static JFrame f ;
44+ static JFrame f ;
45+ static RandomMapGenerator generator ;
46+ static boolean tLOptimized ;
47+
48+ static boolean stopped = true ;
49+
4350 public static void main (String [] args ) throws InterruptedException {
4451
4552 gui .setBorder (new EmptyBorder (2 , 3 , 2 , 3 ));
4653 gui .setBackground (Color .WHITE .darker ().darker ());
47- RandomMapGenerator generator = new RandomMapGenerator (size , cars );
54+ generator = new RandomMapGenerator (size , cars );
4855
4956 map = generator .generateMap ();
5057 int w = map .getLengthX ();
@@ -73,7 +80,9 @@ public static void main(String[] args) throws InterruptedException {
7380 private static void run () throws InterruptedException {
7481 while (run ){
7582 gui .removeAll ();
76- step (map );
83+ if (!stopped ) {
84+ step (map );
85+ }
7786 color (gui , map );
7887 f .pack ();
7988 gui .updateUI ();
@@ -173,17 +182,22 @@ public static void step(Map map) {
173182 //Print out state
174183 iterations ++;
175184 ArrayList <Intersection > intersections =map .getHandler ().intersections ;
176- // if(iterations % 10 == 0 ) { //Toggle all lights every ten iterations
185+ if (tLOptimized ) {
177186 for (int i = 0 ; i < intersections .size (); i ++) {
178-
179-
187+ if (intersections .get (i ).shouldToggle ()) {
188+ intersections .get (i ).toggle ();
189+ }
190+ }
191+ }
192+ else {
193+ if (iterations % 10 == 0 ) { //Toggle all lights every ten iterations
194+ for (int i = 0 ; i < intersections .size (); i ++) {
180195 if (intersections .get (i ).shouldToggle ()) {
181196 intersections .get (i ).toggle ();
182-
183-
197+ }
184198 }
185199 }
186- // }
200+ }
187201 int t =0 ;
188202 while (!haveAllMoved ()&&t <=5 )
189203 {
@@ -224,12 +238,18 @@ private static boolean haveAllMoved()
224238 }
225239 public static void Jbutton (final JFrame f ) {
226240 JToolBar vertical = new JToolBar (JToolBar .VERTICAL );
241+ JButton start = new JButton ("Start" );
227242 JButton stop = new JButton ("Stop" );
228243 JButton step = new JButton ("Step" );
229244
230- stop .addActionListener (new ActionListener () {
245+ start .addActionListener (new ActionListener () {
246+ public void actionPerformed (ActionEvent ae ) {
247+ stopped = false ;
248+ }
249+ });
250+ stop .addActionListener (new ActionListener () {
231251 public void actionPerformed (ActionEvent ae ) {
232- run = false ;
252+ stopped = true ;
233253 }
234254 });
235255
@@ -249,6 +269,7 @@ public void actionPerformed(ActionEvent ae) {
249269 }
250270 });
251271
272+ vertical .add (start );
252273 vertical .add (stop );
253274 vertical .add (step );
254275// add(label, BorderLayout.WEST);
@@ -261,29 +282,69 @@ public void actionPerformed(ActionEvent ae) {
261282 public static void addMenus (final JFrame frame ) {
262283 JMenuBar menubar = new JMenuBar ();
263284 JMenu fileMenu = new JMenu ("File" );
285+ JMenu optimizations = new JMenu ("Optimizations" );
264286 menubar .add (fileMenu );
265-
266- JMenuItem StopItem = new JMenuItem ("Stop" );
267- JMenuItem StartItem = new JMenuItem ("Start (doesnt work yet)" );
268287
269- fileMenu .add (StopItem );
270- StopItem .addActionListener (new ActionListener () {
288+ JMenuItem save = new JMenuItem ("Save" );
289+ JMenuItem load = new JMenuItem ("Load" );
290+ fileMenu .add (save );
291+ fileMenu .add (load );
292+
293+ save .addActionListener (new ActionListener () {
271294 public void actionPerformed (ActionEvent ae ) {
272- run =false ;
295+ String path = getFilePath (frame );
296+ MapIO .saveMap (map , path );
273297 }
274298 });
275- fileMenu .add (StartItem );
276- StartItem .addActionListener (new ActionListener () {
299+ load .addActionListener (new ActionListener () {
277300 public void actionPerformed (ActionEvent ae ) {
278- run = true ;
279- System . out . println ( "should i run again?" + run + " but i dont" );
301+ String path = getFilePath ( frame ) ;
302+ map = MapIO . loadMap ( path );
280303 }
281304 });
305+ JMenu settingsMenu = new JMenu ("Settings" );
306+ JMenuItem StopItem = new JMenuItem ("Stop" );
307+ JMenuItem StartItem = new JMenuItem ("Start (doesnt work yet)" );
308+ JMenuItem minimizeTime = new JMenuItem ("Time" );
309+ JMenuItem minimzeDistance = new JMenuItem ("Distance" );
310+ JMenuItem base = new JMenuItem ("Baseline" );
282311
312+ settingsMenu .add (optimizations );
313+ optimizations .add (base );
314+ optimizations .addActionListener (new ActionListener () {
315+ public void actionPerformed (ActionEvent ae ) {
316+ map .setPath (2 );
317+ }
318+ });
319+ optimizations .add (minimzeDistance );
320+ optimizations .addActionListener (new ActionListener () {
321+ public void actionPerformed (ActionEvent ae ) {
322+ map .setPath (1 );
323+ }
324+ });
325+ optimizations .add (minimizeTime );
326+ optimizations .addActionListener (new ActionListener () {
327+ public void actionPerformed (ActionEvent ae ) {
328+ map .setPath (0 );
329+ }
330+ });
331+
332+ menubar .add (settingsMenu );
283333
334+ final JCheckBoxMenuItem tL = new JCheckBoxMenuItem ("TrafficLight Optimizations" , tLOptimized );
335+ tL .addActionListener (new ActionListener () {
336+ public void actionPerformed (ActionEvent ae ) {
337+ tLOptimized = !tLOptimized ;
338+ tL .setState (tLOptimized );
339+ }
340+ });
341+ settingsMenu .add (tL );
284342 frame .setJMenuBar (menubar );
285343 frame .pack ();
286344
287345 }
288-
346+ public static String getFilePath (JFrame frame ) {
347+ String s = (String )JOptionPane .showInputDialog (frame , "Enter a file path and name" , "File Path" , JOptionPane .PLAIN_MESSAGE , null , null , "maps/map1.map" );
348+ return s ;
349+ }
289350}
0 commit comments