-
Notifications
You must be signed in to change notification settings - Fork 16
/
Lesson_02_Handcoded_GUI_Self_Destructing.java
237 lines (206 loc) · 9.02 KB
/
Lesson_02_Handcoded_GUI_Self_Destructing.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello World with Dr. Dan - A Complete Introduction to Programming from Java to C++ (Code and Course © Dan Grissom)
//
// Additional Lesson Resources from Dr. Dan:
// High-Quality Video Tutorials: www.helloDrDan.com
// Free Commented Code: https://github.com/DanGrissom/hello-world-dr-dan-java
//
// In this lesson you will learn:
// 1) How to create GUIs from scratch (using code and not code-generating GUI builders)
// a) The JFrame class (and extending from it)
// b) Initializing components
// i) Setting window/frame properties
// ii) Container and layout
// iii) Initializing components (https://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html)
// iv) Adding components to container
// b) Initializing events
// i) Initializing listeners (event handlers)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Lesson_02_Handcoded_GUI_Self_Destructing extends JFrame {
// Create random class
private Random randy = new Random();
// Component variables
private JButton b1 = new JButton("Press Me!!");
private JButton b2 = new JButton("You Better Not Press Me!");
// Color constants
private final Color ucrBlue = new Color(45, 108, 192);
private final Color ucrNavy = new Color(0, 48, 102);
private final Color ucrGold = new Color(241, 171, 0);
private final Color ucrGray = new Color(122, 110, 103);
///////////////////////////////////////////////////////////////
// MAIN - Entry point where code will begin execution for file
///////////////////////////////////////////////////////////////
public static void main(String[] args) {
// Simple welcome statements printed to screen
System.out.println("Program Objective: Learn to create GUIs from code (not using a GUI builder) and GUI fundamentals.");
System.out.println("===========================================================================");
// A GUI must instantiate an object of itself (a JFrame) in the main
Lesson_02_Handcoded_GUI_Self_Destructing gui = new Lesson_02_Handcoded_GUI_Self_Destructing();
}
///////////////////////////////////////////////////////////////
// Constructor - Simply calls two methods that do the real work
// Parameters:
// NONE
///////////////////////////////////////////////////////////////
public Lesson_02_Handcoded_GUI_Self_Destructing() {
initComponents();
initEvents();
}
////////////////////////////////////////////////////////////////////////////////
// This method initializes all things related to style and appearance.
// Parameters:
// NONE
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
private void initComponents() {
// Init general properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Dr. Dan's Self-Destructing GUI");
// Get content pane and set layout to FlowLayout
Container c = getContentPane();
c.setLayout(new FlowLayout());
//Init components
c.setBackground(ucrGray);
b1.setBackground(ucrBlue);
b1.setForeground(Color.WHITE);
b2.setBackground(Color.WHITE);
b2.setForeground(ucrNavy);
b2.setToolTipText("I thought I told you...");
//b1.setEnabled(false);
b2.setVisible(false);
b2.setOpaque(false);
// Add components to the content pane
c.add(b1);
c.add(b2);
// Style 50 more buttons
for(int i = 0; i < 50; i++) {
// Create button
JButton b = new JButton(String.valueOf(i));
// Style button
if (i % 2 == 0) { // Even iteration
b.setBackground(ucrNavy);
b.setForeground(ucrGold);
} else { // Odd iteration
b.setBackground(ucrGold);
b.setForeground(ucrNavy);
}
// Add button to container
c.add(b);
}
// Init size and visibility
setVisible(true);
setBounds(800, 200, 600, 400);
}
////////////////////////////////////////////////////////////////////////////////
// This method creates and initializes all event handlers executed when a
// component (e.g., button) interaction occurs.
// Parameters:
// NONE
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
private void initEvents() {
/////////////////////////////////////////////////////////////////////////////
// Event handler for the b1 button (Press Me)
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Display a pop-up
JOptionPane.showMessageDialog(null, "Congrats! You can follow directions!");
b2.setVisible(true);
}
});
//////////////////////////////////////////////////////////
// Event handler for the b2 button (You better not press...)
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Display countdown pop-ups
JOptionPane.showMessageDialog(null, "Uh oh, you didn't follow directions. This GUI will self-destruct in 5...", "Warning", JOptionPane.WARNING_MESSAGE);
randomizeComponentColors();
JOptionPane.showMessageDialog(null, "4...", "Warning", JOptionPane.WARNING_MESSAGE);
randomizeComponentColors();
JOptionPane.showMessageDialog(null, "3...", "Warning", JOptionPane.WARNING_MESSAGE);
randomizeComponentColors();
JOptionPane.showMessageDialog(null, "2...", "Warning", JOptionPane.WARNING_MESSAGE);
randomizeComponentColors();
JOptionPane.showMessageDialog(null, "1...", "Warning", JOptionPane.WARNING_MESSAGE);
randomizeComponentColors();
// The process to draw the GUI is currently running in the same thread as this code.
// Thus, if we cause this code to slow down by putting huge sleep commands in it,
// the thread will not be able to draw the GUI b/c it will be busy in the FOR loop.
// So, we create a new thread so that the code in this new thread can run concurrently
// with the GUI painting/drawing thread.
new Thread() {
public void run() {
try {
// Get the container that has all the buttons
Container c = getContentPane();
int numComponents = c.getComponentCount();
// Init variables for how often to update the display
int numColorChangesPerButtonDis = 10; // How many times to change the color before making a button disappear
int millisBetweenButtonDis = 200; // Number of milliseconds between each button disappearing
// Iterate through each flash (random color change)
for (int i = 0; i < numComponents * numColorChangesPerButtonDis; i++) {
randomizeComponentColors();
Thread.sleep(millisBetweenButtonDis / numColorChangesPerButtonDis);
// Every numColorChangesPerButtonDis of flashes means it's time for a button to disappear
if (i % numColorChangesPerButtonDis == 0) {
// Grab a random component and remove it
Component comp = c.getComponent(randy.nextInt(c.getComponentCount()));
c.remove(comp);
// When a button is removed, also change the background color
c.setBackground(new Color(randy.nextInt(256), randy.nextInt(256), randy.nextInt(256)));
}
}
// Once all buttons have been removed, close the GUI/program
System.exit(0);
} catch (Exception exp) {
// Do nothing
}
}
}.start();
}
});
/////////////////////////////////////////////////////////////////////////////
// More event handlers for the b2 button (You better not press...)
b2.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) { }
public void mouseEntered(MouseEvent e) { /*b2.setVisible(false);*/ }
public void mouseExited(MouseEvent e) { /*b2.setVisible(true);*/ }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
});
}
////////////////////////////////////////////////////////////////////////////////
// This method obtains all the components in the JFrame's container and changes
// their background and text (foreground) colors to random colors.
// Parameters:
// NONE
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
protected void randomizeComponentColors() {
// Grab the container
Container c = getContentPane();
// Iterate through all the components and change their styles
for (int i = 0; i < c.getComponentCount(); i++) {
Component comp = c.getComponent(i);
comp.setBackground(new Color(randy.nextInt(256), randy.nextInt(256), randy.nextInt(256)));
comp.setForeground(new Color(randy.nextInt(256), randy.nextInt(256), randy.nextInt(256)));
}
// Repaint the GUI so it draws the style changes
repaint();
}
}