🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.java.programmer
3 messages
3 total messages Started by Andy K Fri, 12 Mar 2004 11:16
Problems adding a timer
#274
Author: Andy K
Date: Fri, 12 Mar 2004 11:16
197 lines
7642 bytes
I am trying to simulate a traffic lights system I have done most of the 
work and so far the program is working ok. I need to add a timer so that 
I can show a graphics which shows wait, ready, go accordingly. I am 
having problems with this. Can anyone help please. I have tried  but I 
can only get the changes with button graphics where I have to press. 
Adding sounds would also be great. I am new to Java hence these questions.

Thanks

//package stop;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Applet1
     extends Applet {
   private stoplight stoplight1, stoplight2, stoplight3, 
stoplight4,WaitingMan;
   private Button PushButton1;
   private int status = 0;
   private Button PushButton2;
   //private Button WaitingMan;


   public void init() {
     setLayout(null);
     setBackground(new Color(47, 61, 89));
     stoplight1 = new stoplight(150, 50);
     stoplight2 = new stoplight(250, 150);
     stoplight3 = new stoplight(150, 250);
     stoplight4 = new stoplight(50, 150);
     WaitingMan = new stoplight(350,50);//New Line

     stoplight1.setStatus(0);
     stoplight2.setStatus(2);
     stoplight3.setStatus(0);
     stoplight4.setStatus(2);
     WaitingMan.setStatus(0) ;// New Line

    //Add Left Button for pedestrians
     PushButton1 = new Button("Press to Cross");
     PushButton1.addActionListener(new PushButton1Handler());
     PushButton1.setBounds(0, 75, 95, 20);//Set Button Size & position it
     add(PushButton1);

     //Add Right Button for Pedestrians
     PushButton2 = new Button("Press to Cross");
     PushButton2.addActionListener(new PushButton1Handler());
     PushButton2.setBounds(205, 75, 100, 20);//Set Button Size & position it
     add(PushButton2);

     //Add waiting man
     //WaitingMan = new Button("Please Wait before crossing");
     //WaitingMan.addActionListener(new PushButton1Handler());
     //WaitingMan.setBounds(205, 35, 150, 30);//Set man Size & position it
     //add(WaitingMan);

   }

//Draw the traffic lights and add colors
   public void paint(Graphics g) {

     g.setColor(new Color(200, 200, 200));
     g.fillRect(100, 0, 100, 300);//Main road for cars
     g.fillRect(0, 100, 300, 100);//Zebra crossing
     stoplight1.CheckColor(g);
     stoplight2.CheckColor(g);
     stoplight3.CheckColor(g);
     stoplight4.CheckColor(g);
     WaitingMan.CheckColor(g);
     g.drawRect(250,215,100,40);
     g.drawString("Read Before Crossing the Road",240,210);
   }

   class PushButton1Handler
       implements ActionListener {

     public void actionPerformed(ActionEvent e) {
       status++;
       if (status == 4) {
         status = 0;
       }
       switch (status) {
         case 0:
           stoplight1.setStatus(0);//Car lights
           stoplight2.setStatus(2);//Car lights
           stoplight3.setStatus(0);//pedestrian lights
           stoplight4.setStatus(2);//pedestrian lights
           WaitingMan.setStatus(2);//Wait sign

           break;
         case 1:
           stoplight1.setStatus(0);//Car lights
           stoplight2.setStatus(1);//Car lights
           stoplight3.setStatus(0);//pedestrian lights
           stoplight4.setStatus(1);//pedestrian lights
           WaitingMan.setStatus(1);//Wait Sign
           break;
         case 2:
           stoplight1.setStatus(2);//car lights
           stoplight2.setStatus(0);//Car lights
           stoplight3.setStatus(2);//Pedestrian lights
           stoplight4.setStatus(0);//Pedestrian Lights
           WaitingMan.setStatus(0);//Wait Sign
           break;
         case 3:
           stoplight1.setStatus(1);//Car lights
           stoplight2.setStatus(0);//Car lights
           stoplight3.setStatus(1);//Pedestrian lights
           stoplight4.setStatus(0);//Pedestrian lights
           WaitingMan.setStatus(0);//Wait Sign
           break;

       }
       repaint();
     }
   }


class stoplight {
   private int x;
   private int y;
   private Traffic Lamp[] = {};
   private int status;
   public stoplight(int x, int y) {
     this.x = x;
     this.y = y;
     Lamp = new Traffic[3];
     Lamp[0] = new Traffic(this, 0, x, y - 20);
     Lamp[1] = new Traffic(this, 1, x, y);
     Lamp[2] = new Traffic(this, 2, x, y + 20);
   }

   public void setStatus(int status) {
     this.status = status;
   }

   public int getStatus() {
     return (status);
   }

   public void CheckColor(Graphics g) {
     g.setColor(Color.black);
     g.fillRect(x - 10, y - 30, 20, 60);
     Lamp[0].CheckColor(g, status);
     Lamp[1].CheckColor(g, status);
     Lamp[2].CheckColor(g, status);
   }
}

class Traffic {

   private int type, x, y, status;
   private stoplight CheckTraffic;
   public Traffic(stoplight CheckTraffic, int type, int x, int y) {
     this.type = type;
     this.x = x;
     this.y = y;
   }

   public void CheckColor(Graphics g, int status) {
     switch (type) {
       case 0:
         if (status == 0) {
           g.setColor(Color.red);
           //g.drawString("Wait",255,250);
         }
         else {
           g.setColor(Color.gray);
         }
         break;
       case 1:
         if (status == 1) {
           g.setColor(Color.orange);
           g.drawString("Ready",255,260);
         }
         else {
           g.setColor(Color.gray);
         }
         break;
       case 2:
         if (status == 2) {
           g.setColor(Color.green);
           g.drawString("GO",255,270);
         }
         else {
           g.setColor(Color.gray);
         }
         break;

     }
     g.fillOval(x - 8, y - 8, 16, 16);
   }
}
     }
Re: Problems adding a timer
#275
Author: John Davison
Date: Fri, 12 Mar 2004 18:51
32 lines
2447 bytes
On Fri, 12 Mar 2004 11:16:24 +0000, Andy K wrote:

You should use Swing instead of AWT.  Not that it really
matters here, but if you're going to be programming GUIs in Java, learn
Swing.

You can use a Thread as a timer.  See below.

>    public void init() {
// after a bunch of other code
  Thead t = new Thread {
    public void run() {
      while (true) {
        // do stuff when timer goes off
        stoplight1.doSomething();
        stoplight2.doSomething();
        // etc...
        // then wait for 1 second
        try { Thread.sleep(1000); }
        catch (InterruptedException e) { }
      }
    }
  };
  t.start();
  // other init code
} // end public void init()

Note that it's an inner class.  You said you're new to Java, so if you
don't understand inner classes yet, go do that before you continue
programming in it.

John Davison
Re: Problems adding a timer
#276
Author: Stephan Wehner
Date: Fri, 12 Mar 2004 23:32
243 lines
9198 bytes
The javax.swing.Timer class is made for this. See this example code:

import javax.swing.*;
  public static void main(String[] args) {
// the following two are final because we want to access them
// from the anonymous inner class implementation of ActionListener
    final JFrame frame = new JFrame("test"); 
    final JLabel label = new JLabel("javax.swing.Timer testing.
wait..");
    frame.getContentPane().add(label);

    // A timer with a delay of 1000 ms = 1 second
    javax.swing.Timer timer = new javax.swing.Timer(1000, new
java.awt.event.ActionListener()
    {
      int c = 1;
      public void actionPerformed(java.awt.event.ActionEvent e)
      {
        label.setText("javax.swing.Timer testing " + c++);
        frame.pack();
       
      }
    });
    timer.setInitialDelay(5000); // wait 5000 ms = 5 seconds before
notifying ActionListeners
    frame.pack();
    frame.show();

    timer.start();
  }

Note: there is also a class java.util.Timer. The javax.swing.Timer
class invokes its listeners actionPeformed(..) methods on the Event
Dispatch Thread, so it
is safe to change GUI elements as shown in the example.

Stephan
__________________________________________
Stephan Wehner
Editor, Traffic Life: Passionate Tales and Exit Strategies
An anthology about our car culture and alternatives with
short stories, poems, cartoons and lots of other art
www.trafficlife.com


Andy K <a.kadzombe@londonmet.ac.uk> wrote in message news:<c2s62r$h2v$2@canard.ulcc.ac.uk>...
> I am trying to simulate a traffic lights system I have done most of the 
> work and so far the program is working ok. I need to add a timer so that 
> I can show a graphics which shows wait, ready, go accordingly. I am 
> having problems with this. Can anyone help please. I have tried  but I 
> can only get the changes with button graphics where I have to press. 
> Adding sounds would also be great. I am new to Java hence these questions.
> 
> Thanks
> 
> //package stop;
> 
> import java.awt.*;
> import java.awt.event.*;
> import java.applet.*;
> import javax.swing.*;
> 
> public class Applet1
>      extends Applet {
>    private stoplight stoplight1, stoplight2, stoplight3, 
> stoplight4,WaitingMan;
>    private Button PushButton1;
>    private int status = 0;
>    private Button PushButton2;
>    //private Button WaitingMan;
> 
> 
>    public void init() {
>      setLayout(null);
>      setBackground(new Color(47, 61, 89));
>      stoplight1 = new stoplight(150, 50);
>      stoplight2 = new stoplight(250, 150);
>      stoplight3 = new stoplight(150, 250);
>      stoplight4 = new stoplight(50, 150);
>      WaitingMan = new stoplight(350,50);//New Line
> 
>      stoplight1.setStatus(0);
>      stoplight2.setStatus(2);
>      stoplight3.setStatus(0);
>      stoplight4.setStatus(2);
>      WaitingMan.setStatus(0) ;// New Line
> 
>     //Add Left Button for pedestrians
>      PushButton1 = new Button("Press to Cross");
>      PushButton1.addActionListener(new PushButton1Handler());
>      PushButton1.setBounds(0, 75, 95, 20);//Set Button Size & position it
>      add(PushButton1);
> 
>      //Add Right Button for Pedestrians
>      PushButton2 = new Button("Press to Cross");
>      PushButton2.addActionListener(new PushButton1Handler());
>      PushButton2.setBounds(205, 75, 100, 20);//Set Button Size & position it
>      add(PushButton2);
> 
>      //Add waiting man
>      //WaitingMan = new Button("Please Wait before crossing");
>      //WaitingMan.addActionListener(new PushButton1Handler());
>      //WaitingMan.setBounds(205, 35, 150, 30);//Set man Size & position it
>      //add(WaitingMan);
> 
>    }
> 
> //Draw the traffic lights and add colors
>    public void paint(Graphics g) {
> 
>      g.setColor(new Color(200, 200, 200));
>      g.fillRect(100, 0, 100, 300);//Main road for cars
>      g.fillRect(0, 100, 300, 100);//Zebra crossing
>      stoplight1.CheckColor(g);
>      stoplight2.CheckColor(g);
>      stoplight3.CheckColor(g);
>      stoplight4.CheckColor(g);
>      WaitingMan.CheckColor(g);
>      g.drawRect(250,215,100,40);
>      g.drawString("Read Before Crossing the Road",240,210);
>    }
> 
>    class PushButton1Handler
>        implements ActionListener {
> 
>      public void actionPerformed(ActionEvent e) {
>        status++;
>        if (status == 4) {
>          status = 0;
>        }
>        switch (status) {
>          case 0:
>            stoplight1.setStatus(0);//Car lights
>            stoplight2.setStatus(2);//Car lights
>            stoplight3.setStatus(0);//pedestrian lights
>            stoplight4.setStatus(2);//pedestrian lights
>            WaitingMan.setStatus(2);//Wait sign
> 
>            break;
>          case 1:
>            stoplight1.setStatus(0);//Car lights
>            stoplight2.setStatus(1);//Car lights
>            stoplight3.setStatus(0);//pedestrian lights
>            stoplight4.setStatus(1);//pedestrian lights
>            WaitingMan.setStatus(1);//Wait Sign
>            break;
>          case 2:
>            stoplight1.setStatus(2);//car lights
>            stoplight2.setStatus(0);//Car lights
>            stoplight3.setStatus(2);//Pedestrian lights
>            stoplight4.setStatus(0);//Pedestrian Lights
>            WaitingMan.setStatus(0);//Wait Sign
>            break;
>          case 3:
>            stoplight1.setStatus(1);//Car lights
>            stoplight2.setStatus(0);//Car lights
>            stoplight3.setStatus(1);//Pedestrian lights
>            stoplight4.setStatus(0);//Pedestrian lights
>            WaitingMan.setStatus(0);//Wait Sign
>            break;
> 
>        }
>        repaint();
>      }
>    }
> 
> 
> class stoplight {
>    private int x;
>    private int y;
>    private Traffic Lamp[] = {};
>    private int status;
>    public stoplight(int x, int y) {
>      this.x = x;
>      this.y = y;
>      Lamp = new Traffic[3];
>      Lamp[0] = new Traffic(this, 0, x, y - 20);
>      Lamp[1] = new Traffic(this, 1, x, y);
>      Lamp[2] = new Traffic(this, 2, x, y + 20);
>    }
> 
>    public void setStatus(int status) {
>      this.status = status;
>    }
> 
>    public int getStatus() {
>      return (status);
>    }
> 
>    public void CheckColor(Graphics g) {
>      g.setColor(Color.black);
>      g.fillRect(x - 10, y - 30, 20, 60);
>      Lamp[0].CheckColor(g, status);
>      Lamp[1].CheckColor(g, status);
>      Lamp[2].CheckColor(g, status);
>    }
> }
> 
> class Traffic {
> 
>    private int type, x, y, status;
>    private stoplight CheckTraffic;
>    public Traffic(stoplight CheckTraffic, int type, int x, int y) {
>      this.type = type;
>      this.x = x;
>      this.y = y;
>    }
> 
>    public void CheckColor(Graphics g, int status) {
>      switch (type) {
>        case 0:
>          if (status == 0) {
>            g.setColor(Color.red);
>            //g.drawString("Wait",255,250);
>          }
>          else {
>            g.setColor(Color.gray);
>          }
>          break;
>        case 1:
>          if (status == 1) {
>            g.setColor(Color.orange);
>            g.drawString("Ready",255,260);
>          }
>          else {
>            g.setColor(Color.gray);
>          }
>          break;
>        case 2:
>          if (status == 2) {
>            g.setColor(Color.green);
>            g.drawString("GO",255,270);
>          }
>          else {
>            g.setColor(Color.gray);
>          }
>          break;
> 
>      }
>      g.fillOval(x - 8, y - 8, 16, 16);
>    }
> }
>      }
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads