🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.programmer
Article #276

Re: Problems adding a timer

#276
From: 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);
>    }
> }
>      }
Technical Details
Message-ID: <ed608911.0403121532.27c8ee4@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
References: <c2s62r$h2v$2@canard.ulcc.ac.uk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10fd6d,90c614eee4caf871
X-Google-Attributes: gid10fd6d,public
X-Google-Thread: 10a146,90c614eee4caf871
X-Google-Attributes: gid10a146,public
X-Google-Thread: 1089d0,90c614eee4caf871
X-Google-Attributes: gid1089d0,public
X-Google-Thread: f5533,90c614eee4caf871
X-Google-Attributes: gidf5533,public
X-Google-Thread: 115d3b,90c614eee4caf871
X-Google-Attributes: gid115d3b,public
X-Google-Thread: f74b3,90c614eee4caf871
X-Google-Attributes: gidf74b3,public
X-Google-Thread: 10d861,90c614eee4caf871
X-Google-Attributes: gid10d861,public
X-Google-ArrivalTime: 2004-03-12 15:32:54 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
From: google@conxns.net (Stephan Wehner)
Newsgroups: comp.lang.java.programmer,comp.lang.java.security,alt.www.hotjava,microsoft.public.es.java,comp.lang.java.gui,borland.public.jbuilder.java.language,borland.public.jbuilder.java.api
Subject: Re: Problems adding a timer
Date: 12 Mar 2004 15:32:53 -0800
Organization: http://groups.google.com
Lines: 243
Message-ID: <ed608911.0403121532.27c8ee4@posting.google.com>
References: <c2s62r$h2v$2@canard.ulcc.ac.uk>
NNTP-Posting-Host: 206.12.4.42
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1079134374 10266 127.0.0.1 (12 Mar 2004 23:32:54 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 12 Mar 2004 23:32:54 +0000 (UTC)