Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="TrafficLights" width=500 height=500> </applet> */
public class TrafficLights extends Applet implements ItemListener
{
CheckboxGroup cbg;
Checkbox r,g1,y;
public void init()
{
cbg=new CheckboxGroup();
r=new Checkbox("Red",cbg,false);
g1=new Checkbox("Green",cbg,false);
y=new Checkbox("Yellow",cbg,false);
r.addItemListener(this);
g1.addItemListener(this);
y.addItemListener(this);
add(r);
add(g1);
add(y);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
if(cbg.getSelectedCheckbox()==r)
{
g.setColor(Color.red);
g.fillOval(150, 125,70,70);
}
if(cbg.getSelectedCheckbox()==g1)
{
g.setColor(Color.green);
g.fillOval(150, 125,70,70);
}
if(cbg.getSelectedCheckbox()==y)
{
g.setColor(Color.yellow);
g.fillOval(150, 125,70,70);
}
}
}
THANK YOU ....VERY NICE AND WELL SIMPLIFIED
ReplyDeleteYou are welcome
ReplyDeleteIt was asking main method was missing
ReplyDeleteApplets do not require main method to run. Applets and Servlets do not start their own process. Instead they run inside a container. Therefore, they do no need a static main method (which starts the process), but a way to interact with their container.
Deletegive the same file name which is used in main method
Delete