//Program for implementing Traffic Signals
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=”Signals” width=400 height=250></applet>*/
public class Signals extends Applet implements ItemListener
{
String msg=””;
Checkbox stop,ready,go;
CheckboxGroup cbg;
public void init()
{
cbg = new CheckboxGroup();
stop = new Checkbox(“Stop”, cbg, false);
ready = new Checkbox(“Ready”, cbg, false);
go= new Checkbox(“Go”, cbg, false);
add(stop);
add(ready);
add(go);
stop.addItemListener(this);
ready.addItemListener(this);
go.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg=cbg.getSelectedCheckbox().getLabel();
g.drawOval(165,40,50,50);
g.drawOval(165,100,50,50);
g.drawOval(165,160,50,50);
if(msg.equals(“Stop”))
{
g.setColor(Color.red);
g.fillOval(165,40,50,50);
}
else if(msg.equals(“Ready”))
{
g.setColor(Color.yellow);
g.fillOval(165,100,50,50);
}
else
{
g.setColor(Color.green);
g.fillOval(165,160,50,50);
}
}
}
Output:
showing error
should have a file names signals.java
STEP 1 :-save file as "signals.html"
STEP 2:- javac signals.java
and then
appletviewer signals.html
I found easy code compared to other
super boss
nice
Give result
Hei error in last else statement
I love this site and your articles.
I’m amazed, I have to admit. Rarely do I come across a blog that’s equally educative and
Udemy: An excellent learning resource with a wide range of courses on any subject.