-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrafficlight.java
More file actions
69 lines (64 loc) · 1.9 KB
/
Copy pathtrafficlight.java
File metadata and controls
69 lines (64 loc) · 1.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trafficlight extends JPanel implements ActionListener {
JRadioButton r1, r2, r3;
Color red, yellow, green;
public trafficlight() {
setBounds(0, 0, 640, 480);
r1 = new JRadioButton("Red");
r2 = new JRadioButton("Yellow");
r3 = new JRadioButton("Green");
r1.setSelected(true);
red = Color.red;
yellow = getBackground();
green = getBackground();
ButtonGroup gp = new ButtonGroup();
gp.add(r1);
gp.add(r2);
gp.add(r3);
add(r1);
add(r2);
add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(50, 50, 50, 50);
g.drawOval(50, 110, 50, 50);
g.drawOval(50, 170, 50, 50);
g.setColor(red);
g.fillOval(50, 50, 50, 50);
g.setColor(yellow);
g.fillOval(50, 110, 50, 50);
g.setColor(green);
g.fillOval(50, 170, 50, 50);
}
public void actionPerformed(ActionEvent ae) {
if (r1.isSelected() == true) {
red = Color.red;
yellow = getBackground();
green = getBackground();
} else if (r2.isSelected() == true) {
yellow = Color.yellow;
red = getBackground();
green = getBackground();
} else if (r3.isSelected() == true) {
green = Color.green;
red = getBackground();
yellow = getBackground();
}
repaint();
}
public static void main(String[] args) {
trafficlight c = new trafficlight();
JFrame f = new JFrame();
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
f.add(c);
}
}