-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDiceGUI.java
More file actions
137 lines (106 loc) · 3.24 KB
/
TwoDiceGUI.java
File metadata and controls
137 lines (106 loc) · 3.24 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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoDiceGUI extends JFrame implements ActionListener
{
//Instances Objects/ variables/ consts for this class
JLabel title = new JLabel("Two Dice!");
JLabel subtitle = new JLabel("Lets get the dice rollin:", SwingConstants.CENTER);
JLabel firstRoll = new JLabel("The First Roll is" , SwingConstants.CENTER);
JLabel secondRoll = new JLabel("The Second Roll is");
JLabel result1 = new JLabel("");
JLabel result2 = new JLabel("");
JLabel message = new JLabel(" ");
//Panels to combine first roll and results + second roll and results
//decided not to use because it was hiding the results
/*JPanel panelaAnderson = new JPanel();
JPanel peterPanel = new JPanel();*/
//Button to make all the magic happen
JButton letsRoll = new JButton("Let's Roll!");
//Constructor for the GUI
public TwoDiceGUI()
{
super("Dice em' Up!");
final int WIDTH = 400;
final int HEIGHT = 400;
setSize(WIDTH,HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setLocationRelativeTo(null);//CENTERS THE BOX
//FONTS!!
Font bigFont = new Font("Charcoal CY", Font.BOLD, 67);
Font headlineFont = new Font("Thonburi", Font.BOLD, 29);
Font diceFont = new Font("Ayuthaya", Font.PLAIN, 16);
//created a color instead of using standard
Color purple = new Color(129, 20, 186);
getContentPane().setBackground(purple);
title.setForeground(Color.WHITE);
//add the objects to the page
title.setFont(bigFont);
subtitle.setFont(headlineFont);
//set size of button "letsRoll" + font and color
//letsRoll.setPreferredSize(new Dimension(350, 100));
letsRoll.setFont(new Font("Arial", Font.PLAIN, 40));
letsRoll.setForeground(purple);
letsRoll.setBackground(Color.YELLOW);
message.setFont(headlineFont);
message.setForeground(Color.WHITE);
//the dice
firstRoll.setFont(diceFont);
secondRoll.setFont(diceFont);
result1.setFont(diceFont);
result2.setFont(diceFont);
result1.setForeground(Color.YELLOW);
result2.setForeground(Color.YELLOW);
//add an icon!!
Image icon = Toolkit.getDefaultToolkit().getImage("Dice.png");
setIconImage(icon);
add(title);
add(subtitle);
add(firstRoll);
add(result1);
add(secondRoll);
add(result2);
add(letsRoll);
add(message);
//adding the action listener to the button now
letsRoll.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
rollTheDice();
}
//method that handles everything else
public void rollTheDice()
{
// objects
Die firstDie = new Die();
Die secondDie = new Die();
// variables and constants
int value1 = firstDie.getValue();
int value2 = secondDie.getValue();
String mess;
// calculation phase
if(value1 == value2)
{
mess = "The dice are a match!";
}
else
{
if(value1 > value2)
{
mess = "First die roll is greater.";
}
else
{
mess = "Second die roll is greater.";
}
}
// output phase
message.setText("" + mess);
result1.setText(": " + value1);
result2.setText(": " + value2);
}
}