Write a java program that simulates a traffic light. The program lets the user select one of the three lights: red, yellow, or, green with radio buttons. On selecting a button, an appropriate message with “stop” initially, there is no message shown.

Program Code:
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Trafficlights extends Applet implements ItemListener{
String msg="";
Checkbox red,yellow,green;
CheckboxGroup cg=null;
public void init()
{
cg=new CheckboxGroup();
Checkbox red=new Checkbox("red",cg,true);
red.setBackground(Color.red);
Checkbox yellow=new Checkbox("yellow",cg,false);
yellow.setBackground(Color.yellow);
Checkbox green=new Checkbox("green",cg,false);
green.setBackground(Color.green);
add(red);
add(yellow);
add(green);
red.addItemListener(this);
yellow.addItemListener(this);
green.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
Checkbox chk=cg.getSelectedCheckbox();
g.drawString(chk.getLabel()+" Is selected",101,70);
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: