Skip to main content

Simulates a Traffic Light

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="traffic.class" width=500 height=500>
</applet>
*/
public class traffic extends Applet implements ActionListener
{
 int i=0; 
 Button R,O,G;
 public void init()
 {
  setBackground(Color.white);
  setForeground(Color.black);
  R=new Button("red");
  O=new Button("orange");
  G=new Button("green");
  add(R);
  add(O);
  add(G);
  R.addActionListener(this);
  O.addActionListener(this);
  G.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
  String s=ae.getActionCommand();
  if(s.equals("red"))
   i=1;
  if(s.equals("orange"))
   i=2;
  if(s.equals("green"))
   i=3;
  repaint();
 }
 public void paint(Graphics g)
 {
  g.setColor(Color.yellow);
  g.drawRect(50,50,100,200);
  g.fillRect(50,50,100,200);
  g.setColor(Color.black);
  g.drawOval(80,70,30,30);
  g.drawOval(80,130,30,30);
  g.drawOval(80,190,30,30);
  g.setColor(Color.yellow);
  g.drawLine(100,250,100,900);

  if(i==1)
  {
   g.setColor(Color.red);
   g.fillOval(80,70,30,30);
  }
  if(i==2)
  {
   
   g.setColor(Color.orange);
   g.fillOval(80,130,30,30);
  }
  if(i==3)
  {
   g.setColor(Color.green);
   g.fillOval(80,190,30,30);
  }
 }
}

Comments

Popular posts from this blog

Find Value of S=ut+1/2*a*t**2.

PROCEDURE:-        1.enter values for u,a,t to find distance        2.find distance with the formulae ut+1/2at 2        3.print the above result CODE:- #include<stdio.h> #include<conio.h> void main() {   float u,t,a,S;   clrscr();   printf(“enter values u,t,a”);   scanf(“%f %f %f”, &u,&t,&a);   S=(u*t)+(0.5*a*t*t);   printf(“\n  S = %f”, S); } Input:- enter values u,t,a               U=10,t=4,a=4.9 Output:- S =79.200