-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinFlipper2.java
More file actions
104 lines (84 loc) · 2.44 KB
/
CoinFlipper2.java
File metadata and controls
104 lines (84 loc) · 2.44 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
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
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import sun.audio.*;
import java.io.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class CoinFlipper2 extends JFrame
{
private JButton item1;
public CoinFlipper2()
{
super("Coin Flipper");
setLayout(new FlowLayout());
item1 = new JButton("Flip Coin");
add(item1);
Handler h = new Handler();
item1.addActionListener(h);
}
public void music()
{
URL url = getClass().getResource("/CF.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
}
private class Handler implements ActionListener
{
JFrame frame2 = new JFrame("Coin Flip");
int count = 1;
public void actionPerformed(ActionEvent event)
{
JLabel label1 = new JLabel ("It's Heads!");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JLabel label2 = new JLabel ("It's Tails!");
JLabel label3 = new JLabel("");
JLabel label4 = new JLabel("");
JLabel label5 = new JLabel("Number of Times Flipped: " + count);
Image img1 = new ImageIcon(this.getClass().getResource("/heads.png")).getImage();
Image img2 = new ImageIcon(this.getClass().getResource("/tails.png")).getImage();
music();
label3.setIcon(new ImageIcon(img1));
label4.setIcon(new ImageIcon(img2));
int num = (int) (Math.random() * 2);
if (num == 0)
{
frame2.add(panel);
panel.add(label1);
panel.add(Box.createRigidArea(new Dimension(0, 50)));
panel.add(label3);
panel.add(Box.createRigidArea(new Dimension(0, 50)));
panel.add(label5);
frame2.setVisible(true);
frame2.setSize(250,400);
count++;
}
else
{
frame2.add(panel);
panel.add(label2);
panel.add(Box.createRigidArea(new Dimension(0, 50)));
panel.add(label4);
panel.add(Box.createRigidArea(new Dimension(0, 50)));
panel.add(label5);
frame2.setVisible(true);
frame2.setSize(250,400);
count++;
}
}
}
}