home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unal2i6j / contest / graph.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  21.9 KB  |  670 lines

  1. // Traffic simulation,  Kelly Liu, Feb, 1996
  2. //Tel: (508) 647-7662 (o)   (508) 875-2973
  3.  
  4. import java.io.InputStream;
  5. import java.net.URL;
  6.  
  7. import java.util.*;
  8. import java.awt.*;
  9. import java.applet.Applet;
  10. class Node {
  11.     double x;
  12.     double y;
  13.     int lane;
  14.     int road;
  15.     double dx;
  16.     double dy;
  17.     String lbl;
  18.     int carW;
  19.     int carL;
  20.     double carWaiting;
  21. }
  22.  
  23. class ChangeLight implements Runnable {
  24.     int signal;
  25.     int pauss, redpauss, greenpauss;
  26.     Thread lighter;
  27.  
  28.     ChangeLight(){
  29.        signal=1;
  30.        redpauss=6000;
  31.        greenpauss=6000;
  32.    }
  33.  
  34. public void run() {
  35.               signal=1;
  36.     while (true) {
  37.         if (signal==1){
  38.                       signal=0;
  39.                       pauss=greenpauss;
  40.                   }
  41.                   else {
  42.                       signal=1;
  43.                       pauss=redpauss;
  44.                   }
  45.         try {
  46.         Thread.sleep(pauss);
  47.         } catch (InterruptedException e) {
  48.         break;
  49.         }
  50.     }
  51.     }
  52.     public void start() {
  53.     lighter = new Thread(this);
  54.     lighter.start();
  55.     }
  56.     public void stop() {
  57.     lighter.stop();
  58.     }
  59. }  
  60.  
  61. class CalFlow implements Runnable {
  62.     int carnum, count;
  63.     double carwt;
  64.     int pauss;
  65.     double time0, time1, timelap;
  66.     double carflow[] = new double[40];
  67.  
  68.     Thread flow;
  69.  
  70.     CalFlow(){
  71.       carnum=0;
  72.       carwt=0;
  73.       pauss=2000;
  74.       time0=0;
  75.       time1=0;
  76.       for (int k=0; k<40; k++)
  77.          carflow[k]=0;
  78.       count=0;
  79.    }
  80.  
  81.     public void run() { 
  82.     while (true) {  
  83.                  time1= System.currentTimeMillis();
  84.                  timelap=time1-time0;
  85.                  if (timelap >50) 
  86.          carflow[count]= ((double)(carnum)/timelap)*1000; 
  87.                  count=(count+1)%40;
  88.  
  89.         try {
  90.         Thread.sleep(pauss);
  91.         } catch (InterruptedException e) {
  92.         break;
  93.         }
  94.     }
  95.     }
  96.     public void start() {
  97.     flow = new Thread(this);
  98.     flow.start();
  99.     }
  100.     public void stop() {
  101.     flow.stop();
  102.     }
  103. }  
  104.  
  105.  
  106. class GraphPanel extends Applet implements Runnable {
  107.     Graph graph;
  108.     int nnodes;
  109.     int lghtOrStp;
  110.     Node nodes[] = new Node[100];
  111.     ChangeLight  light[] = new ChangeLight[5];
  112.     CalFlow carpermin[] = new CalFlow[5];
  113.     Thread relaxer, flow;
  114.     int brgflag[] = new int[5];
  115.     double speed=10;
  116.     int carwidth=6, carlength=9;
  117.     int xpos[]= new int[5];
  118.     int ypos=200;
  119.     int brgright[] = new int[5];
  120.     int brgleft[] = new int[5]; 
  121.     int brgtop =ypos+ carlength; 
  122.     int brgbottom=ypos- carlength;
  123.     int rdleft[]=new int[5];
  124.     int rdright[] = new int[5];
  125.     int rdtop= ypos+ carwidth, rdbottom= ypos- carwidth;
  126.  
  127.     GraphPanel(Graph graph) {
  128.  
  129.  
  130.               lghtOrStp=1;                                    //stop :0, light: 1
  131.     this.graph = graph;
  132.               for (int i=0; i<5; i++)
  133.               {
  134.                light[i]= new ChangeLight();
  135.                carpermin[i]= new CalFlow();
  136.            
  137.                xpos[i]=150*(i+1);
  138.                brgright[i]=xpos[i]- carlength;
  139.                brgleft[i]=xpos[i]+ carlength;
  140.                brgflag[i]=0;
  141.                 }
  142.                for(int k=1; k<4; k++){
  143.                   rdleft[k]= xpos[k-1]- carwidth;
  144.                   rdright[k]= xpos[k-1]+ carwidth; 
  145.                }
  146.               rdleft[0]=0;
  147.               rdright[0]=0;
  148.       }
  149.  
  150.     int findNode(String lbl) {
  151.     for (int i = 0 ; i < nnodes ; i++) {
  152.         if (nodes[i].lbl.equals(lbl)) {
  153.         return i;
  154.         }
  155.     }
  156.     return addNode(lbl);
  157.     }
  158.     int addNode(String lbl) {
  159.                int temp;
  160.      Node n = new Node();
  161.                temp = (int)(5*Math.random());
  162.                if (temp==0||temp==4){
  163.                      n.x = 480 + 210*Math.random();
  164.                      n.y= ypos;
  165.                      n.carW=carlength;
  166.                      n.carL=carwidth;
  167.                 }
  168.                else{
  169.                      n.x= xpos[temp-1];
  170.                      n.y= 10+100*Math.random();
  171.                      n.carW=carwidth;
  172.                      n.carL=carlength;
  173.                 }
  174. //              temp=(int)(3*Math.random());                   // three lanes
  175. //        n.y = 150+50*temp;
  176.                if (temp==4)
  177.                    temp=0;
  178.                n.road=temp;
  179.       n.lbl = lbl;
  180.                n.carWaiting=-1;
  181.      nodes[nnodes] = n;
  182.      return nnodes++;
  183.     }
  184.  
  185. public void run() {
  186.               for (int j=0; j<5; j++)
  187.                   light[j].signal=1;
  188.               
  189.               flow = new Thread(carpermin[0]);   
  190.               carpermin[0].time0 = System.currentTimeMillis();
  191.               carpermin[0].carnum=0;
  192.               flow.start();
  193.     while (true) {
  194.         relax();
  195.         try {
  196.         Thread.sleep(50);
  197.         } catch (InterruptedException e) {
  198.         break;
  199.         }
  200.     }
  201.     }
  202.  
  203.     synchronized void relax() {
  204.     for (int i = 0 ; i < nnodes; i++) {     
  205.                   if (nodes[i].road==0){
  206.                     nodes[i].dx = -speed*Math.random();
  207.           nodes[i].dy = 2*Math.random()-1;
  208.                    }
  209.                   else{
  210.                     nodes[i].dy = speed*Math.random();
  211.           nodes[i].dx = 2*Math.random()-1;
  212.                   }
  213.     }
  214.     for (int i = 0 ; i < nnodes ; i++) {
  215.         Node n1 = nodes[i];
  216.         double dx = 0;
  217.         double dy = 0;
  218.         for (int j = 0 ; j < nnodes ; j++) {
  219.                             Node n2 = nodes[j];
  220.         if (i == j||n1.road!=n2.road) {
  221.             continue;
  222.         }
  223.                             double vx;
  224.         if(n1.road==0)
  225.             vx = n1.x - n2.x;
  226.                             else
  227.                                 vx= n2.y-n1.y;
  228.                             if (vx<0)
  229.                                  continue;
  230.                             double len=vx;
  231.                              
  232.                             if( len<(n2.carW+n2.carL)){
  233.                                 if (n1.carWaiting<0) 
  234.                                   n1.carWaiting= System.currentTimeMillis();
  235.                                
  236.                                  if(n1.road==0)
  237.                                       n1.dx=0;
  238.                                  else
  239.                                       n1.dy=0;
  240.                               }                                 
  241.         }
  242.     
  243.     }         
  244. //move a car
  245.      Dimension d = size();
  246.                double temp;
  247.                    for (int i = 0 ; i < nnodes ; i++) {
  248.             Node n = nodes[i];
  249.                        if(n.road==0){ 
  250.                           temp=n.x;
  251.                 n.x += Math.max(-10, Math.min(10, n.dx));
  252.                           for (int k=0; k<3; k++){
  253.                               if ((n.x<brgleft[k]&&n.x>brgright[k])&&brgflag[k]==1){  
  254.                                 if(temp> brgleft[k] ||temp<brgright[k])
  255.                                     n.x=temp;
  256.                               }
  257.                              else if ((n.x< brgleft[k] &&n.x>brgright[k])&&brgflag[k]==0)
  258.                                 if (lghtOrStp==0)
  259.                                     brgflag[k]=1;
  260.                                 else{
  261.                                     if (light[k].signal==0)
  262.                                         brgflag[k]=1;
  263.                                     else
  264.                                         n.x=temp;
  265.                                 }
  266.                              else if(temp< brgleft[k] &&temp>brgright[k])
  267.                                  brgflag[k]=0;
  268.         if (n.x < 0) {
  269.             n.x = d.width-10*Math.random();
  270.                                 carpermin[0].carnum=carpermin[0].carnum+1;
  271.         } else if (n.x > d.width) {
  272.             n.x = d.width-10*Math.random();
  273.         }
  274.                              if (n.x!=temp&&n.carWaiting==-1){
  275.                                    carpermin[0].carwt+= System.currentTimeMillis()-
  276. n.carWaiting;
  277.                                    n.carWaiting=-1;
  278.                              }
  279.                             }
  280.                       }
  281.                     else{
  282.                           temp=n.y;
  283.                 n.y += Math.max(-10, Math.min(10, n.dy));
  284.                           if ((n.y<brgtop&&n.y>brgbottom)&&brgflag[n.road-1]==1){  
  285.                              if(temp> brgtop ||temp<brgbottom)
  286.                                 n.y=temp;     
  287.                            }
  288.                           else if ((n.y< brgtop &&n.y>brgbottom)&&brgflag[n.road-1]==0)
  289.                                     if (lghtOrStp==0)
  290.                                          brgflag[n.road-1]=1;
  291.                                     else{
  292.                                          if (light[n.road-1].signal==1)
  293.                                              brgflag[n.road-1]=1; 
  294.                                          else
  295.                                              n.y=temp;
  296.                                       }
  297.                           else if(temp< brgtop &&temp>brgbottom)
  298.                              brgflag[n.road-1]=0;
  299.  
  300.         if (n.y > d.height||n.y<0) {
  301.             n.y = 10*Math.random();
  302.                                   carpermin[0].carnum=carpermin[0].carnum+1;
  303.         }
  304.                       }
  305.     }
  306.     repaint();
  307.     }
  308.  
  309.     Node pick;
  310.     double pickoldx, pickoldy;
  311.     Image offscreen;
  312.     Dimension offscreensize;
  313.     Graphics offgraphics;
  314.  
  315.     final Color selectColor = Color.pink;
  316.     final Color edgeColor = Color.black;
  317.     final Color nodeColor = new Color(250, 220, 100);
  318.  
  319.     public void paintNode(Graphics g, Node n) {
  320.     int x = (int)n.x;
  321.     int y = (int)n.y;
  322.     g.setColor((n==pick) ? selectColor : nodeColor); 
  323.               int w= n.carW;                             
  324.               int h=n.carL;
  325.     g.fillRect(x - w/2, y - h / 2, w, h);
  326.     g.setColor(Color.black);
  327.     g.drawRect(x - w/2, y - h / 2, w-1, h-1);
  328.               g.drawString(".", x-w/2+2, y+h/2-2);
  329.     }
  330.  
  331.     public void paintRoad(Graphics g){
  332.           Dimension d = size();
  333.           g.setColor(Color.gray);
  334.           for(int k=1; k<4; k++){
  335.                g.drawLine(rdleft[k], 0, rdleft[k], rdbottom);
  336.                g.drawLine(rdleft[k], rdtop, rdleft[k], d.height);
  337.                g.drawLine(rdright[k], 0, rdright[k], rdbottom);
  338.                g.drawLine(rdright[k], rdtop, rdright[k], d.height);
  339.                g.drawLine(rdright[k-1], rdtop, rdleft[k], rdtop);
  340.                g.drawLine(rdright[k-1], rdbottom, rdleft[k], rdbottom);
  341.               }
  342.           g.drawLine(rdright[3], rdbottom, d.width, rdbottom);
  343.           g.drawLine(rdright[3], rdtop, d.width, rdtop);   
  344.     }
  345.  
  346. public void paintLghtPeriod(Graphics g){
  347.      Font warnFont, dispFont, stopFont;
  348.      warnFont=new Font("Arial", Font.BOLD, 20);
  349.      dispFont=new Font("TimesRoman", 0, 12);
  350.      stopFont=new Font("TimesRoman", Font.BOLD, 14);
  351.      Dimension d = size();
  352.  
  353.      offgraphics.setColor(Color.black);   
  354.               if(lghtOrStp==1){
  355.                  offgraphics.drawString("Traffic Light Period (1: red, 0: green)",  600,50);
  356.                  offgraphics.setColor(Color.red);
  357.                  offgraphics.drawString("red", 714, 50);
  358.                  offgraphics.setColor(Color.green);
  359.                  offgraphics.drawString("green", 747, 50);
  360.                  offgraphics.setColor(Color.black);
  361.                  for(int k=0; k<3; k++){ 
  362.                    int tempred= light[k].redpauss/200, tempgreen= light[k].greenpauss/200;
  363.                    int temp1=rdright[3]+170;    
  364.                    int temp2, temp3, temp4, temp5;
  365.                    if (light[k].signal==0){
  366.                         temp2=temp1+tempred;
  367.                         temp3=30*(k+1)+40;
  368.                         temp4= temp2+tempgreen;
  369.                         temp5=temp3+12;
  370.                    }
  371.                   else{
  372.                         temp2=temp1+tempgreen;
  373.                         temp3=30*(k+1)+40+12;
  374.                         temp4= temp2+tempred;
  375.                         temp5=temp3-12;
  376.                  }
  377.                  offgraphics.drawString("Light " +Integer.toString(k+1), temp1-40, (temp5+temp3)/2+5);
  378.                  while (temp1<d.width){
  379.                      offgraphics.drawLine(temp1, temp3, temp2, temp3);
  380.            offgraphics.drawLine(temp2, temp5, temp4, temp5);
  381.                      offgraphics.drawLine(temp1, temp3, temp1, temp5);
  382.                      offgraphics.drawLine(temp2, temp3, temp2, temp5);
  383.                      temp1=temp4;
  384.                      temp2=temp1+tempred;
  385.                      temp4=temp2+tempgreen;
  386.                    }
  387.                 }
  388.                offgraphics.setColor(Color.lightGray);
  389.                offgraphics.fillRect(rdright[3]+328, 30, 15, 130);
  390.                offgraphics.setColor(Color.gray);
  391.                offgraphics.drawRect(rdright[3]+120, 20, 223, 145);
  392.                offgraphics.setColor(Color.black);
  393.                }
  394.                else{
  395.                  offgraphics.setFont(warnFont);
  396.                  offgraphics.setColor(Color.white);
  397.                  offgraphics.fillOval(rdleft[3]+175, rdtop-160, 70, 70);
  398.                  offgraphics.setColor(Color.red);
  399.                  offgraphics.fillOval(rdleft[3]+180, rdtop-155, 60, 60);
  400.                  offgraphics.setColor(Color.white);
  401.                  offgraphics.drawString("STOP", rdleft[3]+183, rdtop-116);
  402.                  offgraphics.setColor(Color.black);
  403.                  offgraphics.setFont(dispFont);
  404.                  offgraphics.drawString("(California)", rdleft[3]+185, rdtop-50);
  405.                }
  406. }
  407.  
  408. public void paintLights(Graphics g){
  409.         Font dispFont, stopFont;
  410.         dispFont=new Font("TimesRoman", 0, 12);
  411.         stopFont=new Font("TimesRoman", Font.BOLD, 14);
  412.  
  413.         g.setFont(dispFont);
  414.         int lightwidth=15;
  415.         for(int k=1; k<4; k++){
  416.            if(lghtOrStp==0){
  417.              g.setColor(Color.red);
  418.              g.fillOval(rdleft[k]-18, rdtop+4, lightwidth, lightwidth); 
  419.              g.setColor(Color.white);
  420.              g.setFont(stopFont);
  421.              g.drawString("S", rdleft[k]-14, rdtop+16);
  422.              g.setFont(dispFont);
  423.            }
  424.           else{ 
  425.              g.setColor(Color.black);
  426.              g.fillRect(rdleft[k]-18, rdtop+4, lightwidth-2, lightwidth-2); 
  427.              g.setColor(light[k-1].signal==1 ? Color.red : Color.green);
  428.              g.fillOval(rdleft[k]-7, rdtop+6, 6, 9); 
  429.              g.setColor(light[k-1].signal==1 ? Color.green : Color.red);
  430.              g.fillOval(rdleft[k]-16, rdtop+2, 9, 6); 
  431.              g.setColor(Color.black);
  432.  
  433.              g.drawString("Light "+k, rdleft[k]-58, rdtop+17); 
  434.            }
  435.         }    
  436. }
  437.  
  438. public void paintAxies(Graphics g){
  439.               int temp1=610;
  440.               int temp2=350;
  441.               int temp3=temp1+160;
  442.               int temp4= temp2-80;
  443.               offgraphics.setColor(Color.gray);
  444.               offgraphics.drawRect(rdright[3]+120, temp4-35, 220, 130);
  445.               offgraphics.setColor(Color.black);
  446.               offgraphics.drawLine(temp1, temp2, temp3, temp2);
  447.               offgraphics.drawLine(temp3, temp2, temp3-10, temp2-2);
  448.               offgraphics.drawLine(temp3, temp2, temp3-10, temp2+2);
  449.               offgraphics.drawLine(temp1, temp2, temp1, temp4);
  450.               offgraphics.drawLine(temp1, temp4, temp1-2, temp4+10);
  451.               offgraphics.drawLine(temp1, temp4, temp1+2, temp4+10);
  452.               for (int k=1; k<4; k++){
  453.                 int grid=20*k;
  454.                 offgraphics.drawLine(temp1, temp2- grid, temp1+5, temp2- grid);
  455.                 offgraphics.drawString(k+".0", temp1- 20, temp2- grid+5);
  456.                }
  457.               offgraphics.drawString("Time", temp3-10, temp2-10);
  458.               offgraphics.drawString("Traffic Flow ", temp1-20, temp2-95);
  459.               offgraphics.drawString("  (cars/sec.)", temp1-20, temp2-82);
  460.  
  461.               for (int k=0; k<40; k++){
  462.                  if (k>=carpermin[0].count){
  463.                         temp3=1;
  464.                         offgraphics.setColor(Color.gray);
  465.                      }
  466.                  else{
  467.                         temp3=2;
  468.                         offgraphics.setColor(Color.black);
  469.                    }
  470.                  offgraphics.drawRect(k*3+temp1, temp2 - (int)(carpermin[0].carflow[k]*20+1), temp3, temp3);
  471.                  }
  472.    }
  473.  
  474.     public synchronized void update(Graphics g) {
  475.  
  476.     Dimension d = size();
  477.     if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
  478.         offscreen = createImage(d.width, d.height);
  479.         offscreensize = d;
  480.         offgraphics = offscreen.getGraphics();
  481.     }
  482.  
  483.     offgraphics.setColor(getBackground());
  484.     offgraphics.fillRect(0, 0, d.width, d.height);
  485.               paintRoad(offgraphics);
  486. //draw lights
  487.               paintLights(offgraphics);
  488. //draw light period
  489.               paintLghtPeriod(offgraphics);
  490. //draw axies for the flow chart
  491.              paintAxies(offgraphics);
  492. //draw cars             
  493.              for (int i = 0 ; i < nnodes ; i++) {
  494.         paintNode(offgraphics, nodes[i]);
  495.     }               
  496.     g.drawImage(offscreen, 0, 0, null);
  497.     }
  498.  
  499.     public synchronized boolean mouseDown(Event evt, int x, int y) {
  500.     double bestdist = Double.MAX_VALUE;
  501.     for (int i = 0 ; i < nnodes ; i++) {
  502.         Node n = nodes[i];
  503.         double dist = (n.x - x) * (n.x - x) + (n.y - y) * (n.y - y);
  504.         if (dist < bestdist) {
  505.         pick = n;
  506.                              pickoldx=n.x;
  507.                              pickoldy=n.y;
  508.         bestdist = dist;
  509.         }
  510.     }
  511.     pick.x = x;
  512.     pick.y = y;
  513.     repaint();
  514.     return true;
  515.     }
  516.  
  517.     public synchronized boolean mouseDrag(Event evt, int x, int y) {
  518.     pick.x = x;
  519.     pick.y = y;
  520.     repaint();
  521.     return true;
  522.     }
  523.  
  524.     public synchronized boolean mouseUp(Event evt, int x, int y) {
  525.                boolean insidelane;
  526.     pick.x = x;
  527.     pick.y = y;
  528.                insidelane=false;
  529.     for (int k=1; k<4; k++)
  530.                  if (x>rdleft[k]&&x<rdright[k])
  531.                  {
  532.                       pick.road=k;
  533.                       pick.x=xpos[k-1];
  534.                       pick.carW= carwidth;
  535.                       pick.carL= carlength;
  536.                       insidelane=true;
  537.                   }
  538.                  if (!insidelane&&(y<rdtop&&y>rdbottom))
  539.                   {
  540.                       pick.road=0;
  541.                       pick.y=ypos;
  542.                       pick.carW= carlength;
  543.                       pick.carL= carwidth;
  544.                       
  545.                      }
  546.                   else if(!insidelane)
  547.                    {
  548.                      pick.x=pickoldx;
  549.                      pick.y=pickoldy;
  550.                      }
  551.     pick = null;
  552.  
  553.     repaint();
  554.     return true;
  555.     }
  556.  
  557.     public void start() {
  558.     relaxer = new Thread(this);
  559.     relaxer.start();
  560.     }
  561.     public void stop() {
  562.     relaxer.stop();
  563.     }
  564. }
  565.  
  566. public class Graph extends Applet {
  567.     GraphPanel panel;
  568.     int carnum;
  569.     Thread LightThrd[] = new Thread[3];
  570.     public void init() {
  571.     setLayout(new BorderLayout());
  572.  
  573.     panel = new GraphPanel(this);
  574.     add("Center", panel);
  575.  
  576.               carnum = Integer.parseInt(getParameter("carnum"));
  577.               carnum=Math.min(carnum, 70);
  578.               for (int k=0; k<carnum; k++)
  579.                  panel.findNode(Integer.toString(k));
  580.               panel.lghtOrStp=1;
  581.               for (int k=0; k<3; k++){
  582.                 LightThrd[k] = new Thread(panel.light[k]);
  583.                 panel.light[k].redpauss=(k+1)*1000+3000;
  584.                 panel.light[k].greenpauss=panel.light[k].redpauss;
  585.                 LightThrd[k].start();
  586.               }
  587.               panel.carpermin[0].time0 = System.currentTimeMillis();
  588.               panel.carpermin[0].carnum=0;
  589.  
  590.              Panel btpnl=new Panel();
  591.              add("South", btpnl);
  592.                
  593.              btpnl.add(new Button("Start"));
  594.              btpnl.add(new Button("End"));
  595.  
  596.              btpnl.add(new Button("Stop Sign"));
  597.              btpnl.add(new Button("Traffic Light"));
  598.              btpnl.add(new Button("New Schedule for Lights"));
  599. }
  600.  
  601.    public boolean action(Event evt, Object arg){
  602.      if (((Button)evt.target).getLabel().equals("Traffic Light"))
  603.        {
  604.         if (panel.lghtOrStp==0){
  605.           panel.lghtOrStp=1;
  606.           for (int k=0; k<3; k++){
  607.             LightThrd[k] = new Thread(panel.light[k]);
  608.             panel.light[k].redpauss=(k+1)*1000+3000;
  609.             panel.light[k].greenpauss=panel.light[k].redpauss;
  610.             LightThrd[k].start();
  611.             panel.carpermin[0].time0 = System.currentTimeMillis();
  612.             panel.carpermin[0].carnum=0;
  613.           }
  614.          }
  615.        }
  616.      else if  (((Button)evt.target).getLabel().equals("Stop Sign"))
  617.       {
  618.          panel.lghtOrStp=0;
  619.          for(int k=0; k<3; k++){
  620.            if (LightThrd[k].isAlive())
  621.               LightThrd[k].stop();
  622.           }
  623.          panel.carpermin[0].time0 = System.currentTimeMillis();
  624.          panel.carpermin[0].carnum=0;
  625.        }
  626.       else if  (((Button)evt.target).getLabel().equals("End"))
  627.       {
  628.            for(int k=0; k<3; k++){
  629.              if (LightThrd[k].isAlive())
  630.                LightThrd[k].stop();
  631.           } 
  632.            panel.stop();
  633.         }
  634.       else if  (((Button)evt.target).getLabel().equals("Start"))
  635.       {
  636.          if(panel.lghtOrStp==1)
  637.            for(int k=0; k<3; k++){
  638.             if (!LightThrd[k].isAlive()){
  639.                  LightThrd[k] = new Thread(panel.light[k]);
  640.                  LightThrd[k].start();
  641.                 }
  642.           } 
  643.          if (!panel.relaxer.isAlive())
  644.             panel.start();
  645.         }
  646.      else if  (((Button)evt.target).getLabel().equals("New Schedule for Lights"))
  647.       {
  648.          if (panel.lghtOrStp==1){
  649.            for(int k=0; k<3; k++){
  650.              if (LightThrd[k].isAlive()){
  651.                 panel.light[k].redpauss=(int)(Math.random()*6000)+3000;
  652.                 panel.light[k].greenpauss=(int)(Math.random()*6000)+3000;
  653.                }
  654.             }
  655.            panel.carpermin[0].time0 = System.currentTimeMillis();
  656.            panel.carpermin[0].carnum=0;
  657.            panel.carpermin[0].carwt=0;
  658.  
  659.           }
  660.         }
  661.      return true;
  662.     }
  663. public void start() {
  664.     panel.start();
  665. }
  666.     public void stop() {
  667.     panel.stop();
  668.     }
  669. }
  670.