Program that simulates Traffic Light

Aim: 
To write a java program that simulates a traffic light. The program lets the user select one of three lights: red, yellow or green. When a radio button is selected, the light is turned on and only one light can be on at a time. No light is on when the program starts.

Description: 
First create a class by extending Frame class of java.awt package. Override paint () method of Frame class. We can use the method of Graphics class fillOval() to display three lights by setting color. Radio buttons are created such that only one button can be selected at once, based on this selection corresponding fillOval() with color will be displayed.

Program:
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame implements ItemListener
{
Checkbox b1, b2, b3;
CheckboxGroup cbg;
MyFrame()
{
setLayout( new FlowLayout() );
cbg = new CheckboxGroup();
b1 = new Checkbox("RED",cbg,false);
b2 = new Checkbox("GREEN",cbg,false);
b3 = new Checkbox("YELLOW",cbg,false);
add(b1); add(b2); add(b3);
b1.addItemListener(this);
b2.addItemListener(this);
b3.addItemListener(this);
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent ie)
{ repaint();
}
public void paint(Graphics g)
{
if(b1.getState() )
{ g.setColor(Color.red);
g.fillOval(100,100,100,100);
}
if(b2.getState())
{ g.setColor(Color.green);
g.fillOval(100,100,100,100);
}
if(b3.getState())
{ g.setColor(Color.yellow);
g.fillOval(100,100,100,100);
}
}
}
class TLight
{
public static void main(String args[])
{
MyFrame ob = new MyFrame();
ob.setTitle("Traffic Light");
ob.setSize(600, 300);
ob.setVisible(true);
}
}