forked from ranjansharma255/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLight.java
More file actions
53 lines (52 loc) · 1.3 KB
/
Copy pathTrafficLight.java
File metadata and controls
53 lines (52 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code = TrafficSignal width = 500 height = 500></applet>*/
public class TrafficSignal extends Applet implements ItemListener
{
String msg = " ";
Checkbox red,green,yellow;
CheckboxGroup grp;
public void init()
{
grp = new CheckboxGroup();
red = new Checkbox("Red",grp,false);
green = new Checkbox("Green",grp,false);
yellow = new Checkbox("Yellow",grp,false);
add(red);
add(green);
add(yellow);
red.addItemListener(this);
green.addItemListener(this);
yellow.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void paint(Graphics g)
{
msg = grp.getSelectedCheckbox().getLabel();
setBackground(Color.white);
g.drawOval(50, 50, 52, 52);
g.drawOval(50, 103, 52, 52);
g.drawOval(50, 156, 52, 52);
if(msg.equalsIgnoreCase("Green"))
{
g.setColor(Color.green);
g.fillOval(50, 156, 52, 52);
g.drawString("Go",110,190);
}
else if(msg.equalsIgnoreCase("yellow"))
{
g.setColor(Color.yellow);
g.fillOval(50, 103, 52, 52);
g.drawString("Ready",110,135);
}
else if(msg.equalsIgnoreCase("red"))
{
g.setColor(Color.red);
g.fillOval(50, 50, 52, 52);
g.drawString("Stop!",110,85);
}
}