-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkGUI.java
251 lines (225 loc) · 6.52 KB
/
WorkGUI.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//this code produces a GUI in conjunction with the workers in the "WorkNoGUI.java" file
//the GUI does the slow calculation of finding prime numbers up to a number given by the user
//the program can use multiple background threads and the user is prompted to choose the number of threads to spawn.
//the GUI reports on the results of how many primes have been found every few seconds.
//the GUI has a cancel button that can cancel the operation.
//At the end of the calculation, or if the user hits cancel, the results are shown (and the amount of time it took to do the calculation).
package jCode;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class WorkGUI extends JFrame
{
private static final long serialVersionUID = 13L;
//sets up filed for user input
static JTextField input = new JTextField("");
//sets up area for primes to print in scroll pane
static JTextArea output = new JTextArea("");
private JScrollPane outScroll = new JScrollPane(output);
//sets up label for user input
private JLabel directions = new JLabel("<html>Enter an integer below and press start!<html>", SwingConstants.CENTER);
//establishes buttons
static JButton start = new JButton("Start");
private JButton reset = new JButton("Reset");
static JButton cancel = new JButton("Cancel");
//sets label for time
static JLabel time = new JLabel("", SwingConstants.CENTER);
//sets up field to show running progress
static JTextField progress = new JTextField("");
//shows user selected number of threads
private JLabel threads = new JLabel("", SwingConstants.CENTER);
//sets up variable for canceling
static boolean isCancel = false;
private Integer threadCount = 0;
//initiates timer (swing)
Timer timer = new Timer(1000, new timeActionListener());
int timeRun = 0;
//method to reset GUI
public void reset()
{
//sets time back to zero
timeRun = 0;
//clears input, progress, and output
input.setText("");
output.setText("");
progress.setText("");
//puts threads back to 0
threads.setText("Number of Threads: ");
threadCount = 0;
//enables start button
start.setEnabled(true);
}
//keeps a running time
private class timeActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timeRun++;
//shows the time as it is running
time.setText(String.valueOf("Overall GUI running time: " + timeRun));
//if 2 seconds have passed, show progress
if(timeRun % 2 ==0)
{
WorkNoGUI.updater();
}
}
}
//allows cancel button to work
private class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//changes cancel variable to true
isCancel = true;
//timer stops and is reset
timer.stop();
time.setText("");
//cancel message is given in output
WorkNoGUI.canceled();
}
}
//works as manager method
public class ctrlSub implements Runnable
{
int userNum;
int threadCount;
//takes current userNum and threadCount
public ctrlSub(int userNum, int threadCount)
{
this.userNum = userNum;
this.threadCount = threadCount;
}
public void run()
{
try
{
//runs control method
WorkNoGUI.control(userNum, threadCount);
} catch (Exception e) {
e.printStackTrace();
}
timer.stop();
time.setText("Scroll for overall time taken by the thread(s)");
}
}
public void getThreads()
{
//ask user to input thread number to use
while(threadCount == 0)
{
threadCount = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number of threads:"));
//if the number is less than 1, ask to input above 0
if(threadCount < 1)
{
threadCount = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number greater than 0!"));
}
threads.setText("Number of Threads: " + threadCount.toString());
}
//ask user for the number
int userNum = Integer.parseInt(input.getText());
//start the timer
timer.start();
//start thread on ctrlSub() method
new Thread(new ctrlSub(userNum, threadCount)).start();
}
//enables start button functions
private class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
isCancel = false;
start.setEnabled(false);
cancel.setEnabled(true);
getThreads();
}
}
//enables reset button
private class ResetActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timer.stop();
reset();
}
}
//adds panel to show the user selected number of threads and the timer
private JPanel timeBar()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 0));
panel.add(threads);
panel.add(time);
return panel;
}
//adds user input with the timeBar() panel
private JPanel inputTime()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 0));
panel.add(input);
panel.add(timeBar());
return panel;
}
//adds the directions label to inputTime() panel
private JPanel north()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 0));
panel.add(directions);
panel.add(inputTime());
return panel;
}
//adds panel to include output scroll and progress
private JPanel center()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 0));
panel.add(progress);
progress.setEditable(false);
panel.add(outScroll);
output.setLineWrap(true);
output.setWrapStyleWord(true);
output.setEditable(false);
return panel;
}
//adds all buttons
private JPanel south()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 3));
panel.add(start);
panel.add(reset);
panel.add(cancel);
start.addActionListener(new StartListener());
cancel.addActionListener(new CancelListener());
reset.addActionListener(new ResetActionListener());
return panel;
}
//establishes GUI framework
public WorkGUI()
{
super("Very slow thing");
setLocationRelativeTo(null);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(north(), BorderLayout.NORTH);
getContentPane().add(center(), BorderLayout.CENTER);
getContentPane().add(south(), BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args)
{
new WorkGUI();
}
}