Article View: comp.lang.java.programmer
Article #274Problems adding a timer
From: Andy K
Date: Fri, 12 Mar 2004 11:16
Date: Fri, 12 Mar 2004 11:16
197 lines
7642 bytes
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);
}
}
}
Technical Details
Message-ID:
<c2s62r$h2v$2@canard.ulcc.ac.uk>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!chcgil2-snh1.gtei.net!news.bbnplanet.com!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!diablo.theplanet.net!zen.net.uk!demorgan.zen.co.uk!newsfeed.kabelfoon.nl!195.129.110.21.MISMATCH!bnewsfeed00.bru.ops.eu.uu.net!bnewsinpeer00.bru.ops.eu.uu.net!lnewsinpeer00.lnd.ops.eu.uu.net!emea.uu.net!feed1.jnfs.ja.net!jnfs.ja.net!ulcc.ac.uk!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,90c614eee4caf871,start X-Google-Attributes: gid1089d0,public X-Google-Thread: f74b3,90c614eee4caf871,start X-Google-Attributes: gidf74b3,public X-Google-Thread: 10fd6d,90c614eee4caf871,start X-Google-Attributes: gid10fd6d,public X-Google-Thread: 10d861,90c614eee4caf871,start X-Google-Attributes: gid10d861,public X-Google-Thread: 10a146,90c614eee4caf871,start X-Google-Attributes: gid10a146,public X-Google-Thread: f5533,90c614eee4caf871,start X-Google-Attributes: gidf5533,public X-Google-Thread: 115d3b,90c614eee4caf871,start X-Google-Attributes: gid115d3b,public X-Google-ArrivalTime: 2004-03-12 03:14:36 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!crtntx1-snh1.gtei.net!chcgil2-snh1.gtei.net!news.bbnplanet.com!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!diablo.theplanet.net!zen.net.uk!demorgan.zen.co.uk!newsfeed.kabelfoon.nl!195.129.110.21.MISMATCH!bnewsfeed00.bru.ops.eu.uu.net!bnewsinpeer00.bru.ops.eu.uu.net!lnewsinpeer00.lnd.ops.eu.uu.net!emea.uu.net!feed1.jnfs.ja.net!jnfs.ja.net!ulcc.ac.uk!not-for-mail From: Andy K <a.kadzombe@londonmet.ac.uk> Newsgroups: comp.lang.java.programmer,comp.lang.java.security,alt.www.hotjava,microsoft.public.es.java,microsoft.public.es.java,comp.lang.java.gui,borland.public.jbuilder.java.language,borland.public.jbuilder.java.api Subject: Problems adding a timer Date: Fri, 12 Mar 2004 11:16:24 +0000 Organization: Posted via ULCC Internet Services Lines: 198 Message-ID: <c2s62r$h2v$2@canard.ulcc.ac.uk> NNTP-Posting-Host: cal36-30.lgu.ac.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.4) Gecko/20031007 X-Accept-Language: en, en-us