Programming / Computing

Traffic Lights Project

This program demonstrates a graphical traffic light system...............................................       

 

The code for the same is as follows...........................................................................

 

 

 

 

import java.awt.*;

import java.util.*;

import java.awt.event.*;

class Screen extends Frame implements Runnable

 {

  int x,y,w,h;

  int light;

  boolean yell;

  Thread timer;

  int s,m;

  Date d;

  Bulb red1;

  Bulb red2;

  Bulb red3;

  Bulb green1;

  Bulb green2;

  Bulb green3;

  Bulb yellow1;

  Bulb yellow2;

  Bulb yellow3;

 

 

  public Screen()

  {

  super("Traffic Light");

  MyWindowAdapter adapter = new MyWindowAdapter(this);

  addWindowListener(adapter);

 

  x=450;

  y=300;

  w=50;

  h=50;

  light = 1;

 

  yell = false;

 

  red1 = new Bulb();

  yellow1 = new Bulb();

  green1 = new Bulb();

 

  red2 = new Bulb();

  yellow2 = new Bulb();

  green2 = new Bulb();

 

  red3 = new Bulb();

  yellow3 = new Bulb();

  green3 = new Bulb();

 

  timer = new Thread(this);

  timer.start();

  }

 

 

  public void run()

  {

  Thread me = Thread.currentThread();

  while(true)

  {

  try

  {

  me.sleep(100);

  }

  catch(InterruptedException e){}

 

  Date d = new Date();

  s=d.getSeconds();

 

  if(s%8==0)

  {

  yell = true;

  repaint();

  }

 

  if(s%10==0)

  {

  if(light!=3)light++;

  else

  light =1;

  yell=false;

  repaint();

  }

  }

  }

 

 

 

  public void paint(Graphics g)

  {

  g.drawLine(0,200,250,200);

  g.drawLine(0,250,250,250);

  g.drawLine(500,250,850,250);

  g.drawLine(500,200,850,200);

  g.drawLine(300,500,300,1024);

  g.drawLine(350,500,350,1024);

 

  g.drawRect(350,385,50,15);

  g.drawRect(370,400,10,175);

 

     g.drawLine(285,60,462,60);

     g.drawLine(285,385,462,385);

     g.drawLine(285,60,285,385);

     g.drawLine(462,60,462,385);

       

  g.drawArc(325,75,100,50,360,180);

  g.drawArc(400,100,50,50,90,-180);

  g.drawArc(300,100,50,50,90,180);

  g.drawArc(325,125,100,50,180,180);

 

  g.drawArc(325,175,100,50,360,180);

  g.drawArc(400,200,50,50,90,-180);

  g.drawArc(300,200,50,50,90,180);

  g.drawArc(325,225,100,50,180,180);

 

  g.drawArc(325,275,100,50,360,180);

  g.drawArc(400,300,50,50,90,-180);

  g.drawArc(300,300,50,50,90,180);

  g.drawArc(325,325,100,50,180,180);

 

  x=350;

  y=100;

  w=50;

  h=50;

 

  red1.glow(x,y,w,h,g,Color.red);

  yellow1.glow(x,y+100,w,h,g,Color.yellow);

  green1.glow(x,y+200,w,h,g,Color.green);

 

  x=275;

  w=25;

 

  red2.glow(x,y,w,h,g,Color.red);

  yellow2.glow(x,y+100,w,h,g,Color.yellow);

  green2.glow(x,y+200,w,h,g,Color.green);

 

  x=450;

  w=25;

 

  red3.glow(x,y,w,h,g,Color.red);

  yellow3.glow(x,y+100,w,h,g,Color.yellow);

  green3.glow(x,y+200,w,h,g,Color.green);

 

  if(light==1)

  {

  red1.putOff();

  yellow1.putOff();

  yellow2.putOff();

  green2.putOff();

  yellow3.putOff();

  green3.putOff();

  }

 

  if(light==2)

  {

  red2.putOff();

  yellow2.putOff();

  yellow1.putOff();

  green1.putOff();

  yellow3.putOff();

  green3.putOff();

  }

 

  if(light==3)

  {

  green1.putOff();

  yellow1.putOff();

  yellow2.putOff();

  green2.putOff();

  yellow3.putOff();

  red3.putOff();

  }

 

 

  if(yell==true)

  {

  if(light==1)

  {

  w=25;

  x=275;

  yellow2.glow(x,y+100,w,h,g,Color.yellow);

  }

 

  if(light==2)

  {

  x=450;

  w=25;

  yellow3.glow(x,y+100,w,h,g,Color.yellow);

  }

 

  if(light==3)

  {

  w=50;

  x=350;

  y=100;

  yellow1.glow(x,y+100,w,h,g,Color.yellow);

  }

  }

  }

 }

 

 

 

class Light

 {

  public static void main(String arg[])

  {

  Screen s=new Screen();

  s.setSize(1000,700);

  s.setVisible(true);

  }

 }

 

 

class MyWindowAdapter extends WindowAdapter

 {

  Screen s;

 

  public MyWindowAdapter(Screen k)

  {

  s = k;

  }

 

  public void windowClosing(WindowEvent we)

  {

  s.setVisible(false);

  System.exit(0);

  }

 }

 

 

class Bulb

 {

  int x,y,w,h;

  Graphics g;

 

  public void glow(int p,int q,int r,int s,Graphics gr,Color col)

  {

  x=p;

  y=q;

  w=r;

  h=s;

  g=gr;

  g.setColor(col);

  g.fillOval(x,y,w,h);

  }

 

  public void putOff()

  {

  Color p = new Color(240,240,240);

  g.setColor(p);

  g.fillOval(x,y,w,h);

  }

 }

 

© 2010 All rights reserved.

Make a free websiteWebnode