-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypingSpeedGUI.java
399 lines (314 loc) · 11 KB
/
TypingSpeedGUI.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TypingSpeedGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TypingSpeedGUI());
}
private final TypingText typingText;
private long startTime;
private JScrollPane showScrollPane;
private JScrollPane typeScrollPane;
private JTextArea showTextArea;
private JTextArea typeTextArea;
private JTextField elapsedTimeField;
private JTextField wpmField;
private JTextField errorsField;
public TypingSpeedGUI() {
this.typingText = new TypingText();
this.startTime = 0L;
}
@Override
public void run() {
JFrame frame = new JFrame("Typing Speed GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createDisplayPanel(), BorderLayout.BEFORE_FIRST_LINE);
frame.add(createShowTextPanel(), BorderLayout.BEFORE_LINE_BEGINS);
frame.add(createTypeTextArea(), BorderLayout.AFTER_LINE_ENDS);
frame.add(createButtonPanel(), BorderLayout.AFTER_LAST_LINE);
Synchronizer synchronizer = new Synchronizer(showScrollPane, typeScrollPane);
showScrollPane.getVerticalScrollBar().addAdjustmentListener(synchronizer);
showScrollPane.getHorizontalScrollBar().addAdjustmentListener(synchronizer);
typeScrollPane.getVerticalScrollBar().addAdjustmentListener(synchronizer);
typeScrollPane.getHorizontalScrollBar().addAdjustmentListener(synchronizer);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createDisplayPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(16f);
JLabel label = new JLabel("Elapsed time");
label.setFont(font);
panel.add(label);
elapsedTimeField = new JTextField(5);
elapsedTimeField.setEditable(false);
elapsedTimeField.setFont(font);
elapsedTimeField.setHorizontalAlignment(JTextField.TRAILING);
panel.add(elapsedTimeField);
label = new JLabel("seconds");
label.setFont(font);
panel.add(label);
panel.add(Box.createHorizontalStrut(20));
label = new JLabel("Errors");
label.setFont(font);
panel.add(label);
errorsField = new JTextField(5);
errorsField.setEditable(false);
errorsField.setFont(font);
panel.add(errorsField);
panel.add(Box.createHorizontalStrut(20));
label = new JLabel("Calculated words per minute");
label.setFont(font);
panel.add(label);
wpmField = new JTextField(5);
wpmField.setEditable(false);
wpmField.setFont(font);
panel.add(wpmField);
return panel;
}
private JPanel createShowTextPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(16f);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("Text to type");
label.setFont(font);
panel.add(label, gbc);
gbc.gridy++;
showTextArea = new JTextArea(10, 40);
showTextArea.setEditable(false);
showTextArea.setFont(font);
showTextArea.setLineWrap(true);
showTextArea.setText(typingText.getText());
showTextArea.setWrapStyleWord(true);
InputMap inputMap = showTextArea.getInputMap();
inputMap.put(KeyStroke.getKeyStroke("control X"), "none");
inputMap.put(KeyStroke.getKeyStroke("control C"), "none");
showScrollPane = new JScrollPane(showTextArea);
panel.add(showScrollPane, gbc);
return panel;
}
private JPanel createTypeTextArea() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(16f);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("Typing area");
label.setFont(font);
panel.add(label, gbc);
gbc.gridy++;
typeTextArea = new JTextArea(10, 40);
typeTextArea.requestFocusInWindow();
typeTextArea.setFont(font);
typeTextArea.setLineWrap(true);
typeTextArea.setWrapStyleWord(true);
typeTextArea.getDocument().addDocumentListener(new StartListener(this));
typeScrollPane = new JScrollPane(typeTextArea);
panel.add(typeScrollPane, gbc);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Font font = panel.getFont().deriveFont(16f);
JButton button = new JButton("Restart Typing Test");
button.setFont(font);
button.addActionListener(new RestartListener(this));
panel.add(button);
button = new JButton("Typing Completed");
button.setFont(font);
button.addActionListener(new TypingEndListener(this));
panel.add(button);
return panel;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public TypingText getTypingText() {
return typingText;
}
public JTextArea getShowTextArea() {
return showTextArea;
}
public JTextArea getTypeTextArea() {
return typeTextArea;
}
public JTextField getElapsedTimeField() {
return elapsedTimeField;
}
public JTextField getWpmField() {
return wpmField;
}
public JTextField getErrorsField() {
return errorsField;
}
public class StartListener implements DocumentListener {
private final TypingSpeedGUI frame;
public StartListener(TypingSpeedGUI frame) {
this.frame = frame;
}
@Override
public void insertUpdate(DocumentEvent event) {
boolean firstTimeSwitch = frame.getStartTime() == 0L;
if (firstTimeSwitch && frame.getTypeTextArea().getText().length() > 0) {
startTimer();
}
}
@Override
public void removeUpdate(DocumentEvent event) {
boolean firstTimeSwitch = frame.getStartTime() == 0L;
if (firstTimeSwitch && frame.getTypeTextArea().getText().length() > 0) {
startTimer();
}
}
@Override
public void changedUpdate(DocumentEvent event) {
boolean firstTimeSwitch = frame.getStartTime() == 0L;
if (firstTimeSwitch && frame.getTypeTextArea().getText().length() > 0) {
startTimer();
}
}
private void startTimer() {
frame.setStartTime(System.currentTimeMillis());
}
}
public class RestartListener implements ActionListener {
private final TypingSpeedGUI frame;
public RestartListener(TypingSpeedGUI frame) {
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent event) {
frame.setStartTime(0L);
frame.getTypeTextArea().setText("");
frame.getElapsedTimeField().setText("");
frame.getErrorsField().setText("");
frame.getWpmField().setText("");
}
}
public class TypingEndListener implements ActionListener {
private final TypingSpeedGUI frame;
public TypingEndListener(TypingSpeedGUI frame) {
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent event) {
long endTime = System.currentTimeMillis();
String time = calculateTimeInSeconds(endTime);
frame.getElapsedTimeField().setText(time);
frame.getErrorsField().setText(calculateErrors());
String wpm = calculateWPM(endTime);
frame.getWpmField().setText(wpm);
}
private String calculateTimeInSeconds(long endTime) {
long typingTime = endTime - frame.getStartTime();
int timeInSeconds = (int) ((typingTime + 500L) / 1000L);
return Integer.toString(timeInSeconds);
}
private String calculateErrors() {
String inputText = frame.getTypingText().getText();
String outputText = frame.getTypeTextArea().getText();
String errors;
if (inputText.length() == outputText.length()) {
int count = 0;
for (int index = 0; index < inputText.length(); index++) {
char a = inputText.charAt(index);
char b = outputText.charAt(index);
count += (a == b) ? 0 : 1;
}
errors = Integer.toString(count);
} else {
errors = Integer.toString(outputText.length());
}
return errors;
}
private String calculateWPM(long endTime) {
long typingTime = endTime - frame.getStartTime();
int timeInSeconds = (int) ((typingTime + 500L) / 1000L);
String outputText = frame.getTypeTextArea().getText();
double cps = (double) outputText.length() / timeInSeconds;
int wpm = (int) Math.round(cps * 12.0);
return Integer.toString(wpm);
}
}
public class Synchronizer implements AdjustmentListener {
JScrollBar v1, h1, v2, h2;
public Synchronizer(JScrollPane sp1, JScrollPane sp2) {
v1 = sp1.getVerticalScrollBar();
h1 = sp1.getHorizontalScrollBar();
v2 = sp2.getVerticalScrollBar();
h2 = sp2.getHorizontalScrollBar();
}
public void adjustmentValueChanged(AdjustmentEvent e) {
JScrollBar scrollBar = (JScrollBar) e.getSource();
int value = scrollBar.getValue();
JScrollBar target = null;
if (scrollBar == v1)
target = v2;
if (scrollBar == h1)
target = h2;
if (scrollBar == v2)
target = v1;
if (scrollBar == h2)
target = h1;
target.setValue(value);
}
}
public class TypingText {
private final String text;
public TypingText() {
// Here's where you'd read a file with several texts.
// I'm going to hard code one text;
this.text = "This is a test of the National Emergency Alert System. "
+ "This system was developed by broadcast and cable operators "
+ "in voluntary cooperation with the Federal Emergency "
+ "Management Agency, the Federal Communications Commission, "
+ "and local authorities to keep you informed in the event of "
+ "an emergency. If this had been an actual emergency an "
+ "official message would have followed the tone alert you "
+ "heard at the start of this message. No action is required.";
}
public String getText() {
return text;
}
}
}