-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.java
172 lines (149 loc) · 4.45 KB
/
traffic.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
// Main class
// Extending JFrame class and
// Implementing ItemListener interface
public class Traffic_Signal
extends JFrame implements ItemListener {
// Setting the buttons for the layout
JRadioButton jr1;
JRadioButton jr2;
JRadioButton jr3;
// Setting the field area
JTextField j1 = new JTextField(10);
ButtonGroup b = new ButtonGroup();
String msg = " ";
// Initially setting the co-ordinates to 0,0,0
int x = 0, y = 0, z = 0;
public Traffic_Signal(String msg)
{
super(msg);
setLayout(new FlowLayout());
// Assigning name to the button declared above
// with help of JRadioButton class
jr1 = new JRadioButton("Red");
jr2 = new JRadioButton("Yellow");
jr3 = new JRadioButton("Green");
jr1.addItemListener(this);
jr2.addItemListener(this);
jr3.addItemListener(this);
add(jr1);
add(jr2);
add(jr3);
b.add(jr1);
b.add(jr2);
b.add(jr3);
add(j1);
// Method 1
// To add a window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
// It haults here itself
System.exit(0);
}
});
}
// Method 2
// To change colors of traffic signal
public void itemStateChanged(ItemEvent ie)
{
// If it is red
if (ie.getSource() == jr1) {
if (ie.getStateChange() == 1) {
// Then display message- Stop
msg = "Stop!";
x = 1;
// Repainting the box with original one
// Practically black
repaint();
}
else {
msg = "";
}
}
// If state is Orange or technically jr2
if (ie.getSource() == jr2) {
if (ie.getStateChange() == 1) {
// Then display message-
// Get ready in waiting state
msg = "Get Ready to go!";
y = 1;
// Again repainting the button
repaint();
}
else {
msg = "";
}
}
// If state is Green
if (ie.getSource() == jr3) {
if (ie.getStateChange() == 1) {
// Then display message- Go
msg = "Go!!";
z = 1;
repaint();
}
else {
msg = "";
}
}
j1.setText(msg);
}
// Method 3
// handling the paint graphics and
// dimensions of the buttons via
// setting co-ordinates
public void paint(Graphics g)
{
g.drawRect(100, 105, 110, 270);
g.drawOval(120, 150, 60, 60);
g.drawOval(120, 230, 60, 60);
g.drawOval(120, 300, 60, 60);
// Case: Red
if (x == 1) {
g.setColor(Color.RED);
g.fillOval(120, 150, 60, 60);
g.setColor(Color.WHITE);
g.fillOval(120, 230, 60, 60);
g.setColor(Color.WHITE);
g.fillOval(120, 300, 60, 60);
x = 0;
}
// Case: Orange
if (y == 1) {
g.setColor(Color.WHITE);
g.fillOval(120, 150, 60, 60);
g.setColor(Color.YELLOW);
g.fillOval(120, 230, 60, 60);
g.setColor(Color.WHITE);
g.fillOval(120, 300, 60, 60);
y = 0;
}
// Case: Green
if (z == 1) {
g.setColor(Color.WHITE);
g.fillOval(120, 150, 60, 60);
g.setColor(Color.WHITE);
g.fillOval(120, 230, 60, 60);
g.setColor(Color.GREEN);
g.fillOval(120, 300, 60, 60);
z = 0;
}
}
// Method 4
// Main driver method
public static void main(String args[])
{
// Creating object of Jframe class inside main()
// method
JFrame jf = new Traffic_Signal("Traffic Light");
// Setting size and making traffic signal
// operational using setVisible() method
jf.setSize(500, 500);
jf.setVisible(true);
}
}