|
1 | | -/* |
2 | | - * To change this license header, choose License Headers in Project Properties. |
3 | | - * To change this template file, choose Tools | Templates |
4 | | - * and open the template in the editor. |
5 | | - */ |
6 | | -package MiniProject_01_TrafficLight_Simulation; |
7 | | - |
8 | | -/** |
9 | | - * |
10 | | - * @author Ravikumar |
11 | | - */ |
12 | | -import java.awt.event.*; |
13 | | -import java.applet.*; |
14 | 1 | import java.awt.*; |
15 | | - |
16 | | -public class TrafficLight extends Applet implements ItemListener { |
17 | | -String msg = ""; |
18 | | -Checkbox red,green,yellow; |
19 | | -CheckboxGroup cbg; |
20 | | -public void init() { |
21 | | - cbg = new CheckboxGroup(); |
22 | | - red = new Checkbox("Red", cbg, false); |
23 | | - green = new Checkbox("Green", cbg, false); |
24 | | - yellow = new Checkbox("Yellow", cbg, false); |
25 | | - add(red); |
26 | | - add(yellow); |
27 | | - add(green); |
28 | | - red.addItemListener(this); |
29 | | - yellow.addItemListener(this); |
30 | | - green.addItemListener(this); |
31 | | -} |
32 | | -public void itemStateChanged(ItemEvent ie) |
33 | | -{ |
34 | | - repaint(); |
35 | | -} |
36 | | -public void paint(Graphics g) |
| 2 | +import java.applet.*; |
| 3 | +import java.awt.event.*; |
| 4 | +/*<applet code = TrafficSignal width = 500 height = 500></applet>*/ |
| 5 | +public class TrafficSignal extends Applet implements ItemListener |
37 | 6 | { |
38 | | - Color color; |
39 | | - color=Color.BLACK; |
40 | | - g.setColor(color); |
41 | | - g.drawOval(50, 50, 52, 52); |
42 | | - g.drawOval(50, 103, 52, 52); |
43 | | - g.drawOval(50, 156, 52, 52); |
44 | | - String col = cbg.getSelectedCheckbox().getLabel(); |
45 | | - System.out.println(col); |
46 | | -if(col.equalsIgnoreCase("Green")) |
47 | | -{ |
48 | | - color= Color.GREEN; |
49 | | - g.setColor(color); |
50 | | - g.fillOval(50, 156, 52, 52); |
51 | | - g.drawString("GO",110,190); |
52 | | -} |
53 | | -if(col.equalsIgnoreCase("Red")) |
54 | | -{ |
55 | | - color=Color.RED; |
56 | | - g.setColor(color); |
57 | | - g.fillOval(51, 51, 51, 51); |
58 | | - g.drawString("STOP",110,80); |
59 | | -} |
60 | | -if(col.equalsIgnoreCase("Yellow")) |
61 | | -{ |
62 | | - color=Color.YELLOW; |
63 | | - g.setColor(color); |
64 | | - g.fillOval(50, 103, 51, 51); |
65 | | - color= Color.BLACK; |
66 | | - g.setColor(color); |
67 | | - g.drawString("READY",110,140); |
68 | | -} |
69 | | -} |
70 | | -} |
71 | | - |
72 | | - |
| 7 | + String msg = " "; |
| 8 | + Checkbox red,green,yellow; |
| 9 | + CheckboxGroup grp; |
| 10 | + public void init() |
| 11 | + { |
| 12 | + grp = new CheckboxGroup(); |
| 13 | + red = new Checkbox("Red",grp,false); |
| 14 | + green = new Checkbox("Green",grp,false); |
| 15 | + yellow = new Checkbox("Yellow",grp,false); |
| 16 | + add(red); |
| 17 | + add(green); |
| 18 | + add(yellow); |
| 19 | + red.addItemListener(this); |
| 20 | + green.addItemListener(this); |
| 21 | + yellow.addItemListener(this); |
| 22 | + } |
| 23 | + public void itemStateChanged(ItemEvent e) |
| 24 | + { |
| 25 | + repaint(); |
| 26 | + } |
| 27 | + public void paint(Graphics g) |
| 28 | + { |
| 29 | + msg = grp.getSelectedCheckbox().getLabel(); |
| 30 | + setBackground(Color.white); |
| 31 | + g.drawOval(50, 50, 52, 52); |
| 32 | + g.drawOval(50, 103, 52, 52); |
| 33 | + g.drawOval(50, 156, 52, 52); |
| 34 | + if(msg.equalsIgnoreCase("Green")) |
| 35 | + { |
| 36 | + g.setColor(Color.green); |
| 37 | + g.fillOval(50, 156, 52, 52); |
| 38 | + g.drawString("Go",110,190); |
| 39 | + } |
| 40 | + else if(msg.equalsIgnoreCase("yellow")) |
| 41 | + { |
| 42 | + g.setColor(Color.yellow); |
| 43 | + g.fillOval(50, 103, 52, 52); |
| 44 | + g.drawString("Ready",110,135); |
| 45 | + } |
| 46 | + else if(msg.equalsIgnoreCase("red")) |
| 47 | + { |
| 48 | + g.setColor(Color.red); |
| 49 | + g.fillOval(50, 50, 52, 52); |
| 50 | + g.drawString("Stop!",110,85); |
| 51 | + } |
| 52 | + } |
| 53 | + |
0 commit comments