-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrafficlight.java
More file actions
73 lines (72 loc) · 1.88 KB
/
trafficlight.java
File metadata and controls
73 lines (72 loc) · 1.88 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
70
71
72
73
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
class trafficlight extends JFrame implements ActionListener
{
JButton stop,ready,go;
String msg="";
public trafficlight()
{
setTitle("Traffic Light");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
stop=new JButton();
stop.setText("Stop!");
stop.setBounds(80,120,70,30);
ready=new JButton("Ready");
ready.setBounds(80,180,70,30);
go= new JButton("Go");
go.setBounds(80,240,70,30);
panel.add(stop);
panel.add(ready);
panel.add(go);
stop.addActionListener(this);
ready.addActionListener(this);
go.addActionListener(this);
add(panel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==stop)
{
msg="stop";
}
else if(e.getSource()==ready)
{
msg="ready";
}
else if(e.getSource()==go)
{
msg="go";
}
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
g.drawOval(165, 140,50,50);
g.drawOval(165, 200,50,50);
g.drawOval(165, 260,50,50);
if(msg.equals("stop"))
{
g.setColor(Color.RED);
g.fillOval(165, 140,50,50);
}
else if(msg.equals("ready"))
{
g.setColor(Color.YELLOW);
g.fillOval(165, 200,50,50);
}
else if(msg.equals("go"))
{
g.setColor(Color.GREEN);
g.fillOval(165, 260,50,50);
}
}
public static void main(String[] args) {
trafficlight tf=new trafficlight();
tf.setVisible(true);
}
}