-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactionTestingGUI.java
372 lines (289 loc) · 10.4 KB
/
ReactionTestingGUI.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
package com.ggl.testing;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class ReactionTestingGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ReactionTestingGUI());
}
private final DrawingPanel drawingPanel;
private JScrollPane scrollPane;
private final ReactionTableModel tableModel;
private final ReactionTestingModel model;
public ReactionTestingGUI() {
this.model = new ReactionTestingModel();
this.tableModel = new ReactionTableModel();
this.drawingPanel = new DrawingPanel(this, model);
}
@Override
public void run() {
JFrame frame = new JFrame("Reaction Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTablePanel(), BorderLayout.WEST);
frame.add(drawingPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
System.out.println("Frame size: " + frame.getSize());
}
private JPanel createTablePanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
String[] columns = { "Expected Time", "Actual Time", "Difference" };
for (String columnName : columns) {
tableModel.addColumn(columnName);
}
JTable table = new JTable(tableModel);
DecimalFormatRenderer dfr = new DecimalFormatRenderer();
table.getColumnModel().getColumn(0).setCellRenderer(dfr);
table.getColumnModel().getColumn(1).setCellRenderer(dfr);
table.getColumnModel().getColumn(2).setCellRenderer(dfr);
scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
public void addResponse(Reaction reaction) {
Object[] rowData = new Object[3];
rowData[0] = reaction.getExpectedTime();
rowData[1] = reaction.getActualTime();
rowData[2] = reaction.getAbsoluteDifference();
tableModel.insertRow(0, rowData);
scrollPane.getViewport().setViewPosition(new Point(0,0));
}
public void repaint() {
drawingPanel.repaint();
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private final int margin, drawingHeight, drawingWidth;
private final ReactionTestingModel model;
public DrawingPanel(ReactionTestingGUI view, ReactionTestingModel model) {
this.model = model;
this.margin = 40;
this.drawingHeight = 200;
this.drawingWidth = 400;
int width = drawingWidth + margin + margin;
int height = drawingHeight + margin + margin;
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(width, height));
this.addMouseListener(new PanelListener(view, model));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
int rectangleHeight = paintRectangle(g2d);
double expectedTime = model.getCurrentReaction().getExpectedTime();
if (expectedTime > 0.0) {
int textDisplacement = 30;
int x = margin;
int y = margin + rectangleHeight + textDisplacement;
paintExpectedTime(g2d, x, y, expectedTime);
double actualTime = model.getCurrentReaction().getActualTime();
if (actualTime > 0.0) {
y = paintActualTime(g2d, x, y, textDisplacement, actualTime);
y = paintDifference(g2d, x, y, textDisplacement);
paintBar(g2d, actualTime, rectangleHeight);
}
paintStopLine(g2d, expectedTime, rectangleHeight);
}
}
private int paintRectangle(Graphics2D g2d) {
int x = margin;
int y = margin;
int rectangleHeight = 40;
g2d.setStroke(new BasicStroke(3f));
g2d.setColor(Color.BLACK);
g2d.drawRect(x, y, drawingWidth, rectangleHeight);
return rectangleHeight;
}
private void paintExpectedTime(Graphics2D g2d, int x, int y, double expectedTime) {
String expectedTimeString = "Expected time: " + String.format("%5.2f", expectedTime);
g2d.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
g2d.setColor(Color.BLACK);
g2d.drawString(expectedTimeString, x, y);
}
private int paintActualTime(Graphics2D g2d, int x, int y, int textDisplacement,
double actualTime) {
y += textDisplacement;
String actualTimeString = "Actual time: " + String.format("%5.2f", actualTime);
g2d.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
g2d.setColor(Color.BLACK);
g2d.drawString(actualTimeString, x, y);
return y;
}
private int paintDifference(Graphics2D g2d, int x, int y, int textDisplacement) {
y += textDisplacement;
double difference = model.getCurrentReaction().getAbsoluteDifference();
String differenceTimeString = "Difference: " + String.format("%5.2f", difference);
g2d.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
g2d.setColor(Color.BLACK);
g2d.drawString(differenceTimeString, x, y);
return y;
}
private void paintBar(Graphics2D g2d, double actualTime, int rectangleHeight) {
int x = margin + 1;
int y = margin + 1;
int actualWidth = (int) Math.round(actualTime * 10);
g2d.setColor(Color.GREEN);
g2d.fillRect(x, y, actualWidth, rectangleHeight - 2);
}
private void paintStopLine(Graphics2D g2d, double expectedTime, int rectangleHeight) {
int x = (int) Math.round(expectedTime * 10.0) + margin;
int y1 = margin;
int y2 = margin + rectangleHeight;
float[] dash1 = { 2f, 0f, 2f };
BasicStroke bs1 = new BasicStroke(3f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 1.0f, dash1, 2f);
g2d.setStroke(bs1);
g2d.setColor(Color.RED);
g2d.drawLine(x, y1, x, y2);
}
}
public class DecimalFormatRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
private final JLabel cellLabel = new JLabel();
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
cellLabel.setHorizontalAlignment(JLabel.TRAILING);
cellLabel.setText(String.format("%5.2f", (Number) value));
return cellLabel;
}
}
public class PanelListener extends MouseAdapter {
private final ReactionTestingGUI view;
private final ReactionTestingModel model;
private final Timer timer;
public PanelListener(ReactionTestingGUI view, ReactionTestingModel model) {
this.view = view;
this.model = model;
this.timer = new Timer(40, new TimerListener(view, model));
}
@Override
public void mouseReleased(MouseEvent event) {
if (event.getButton() == MouseEvent.BUTTON1) {
if (model.isRunning()) {
timer.stop();
model.addReaction(model.getCurrentReaction());
view.addResponse(model.getCurrentReaction());
model.setRunning(false);
} else {
Reaction currentReaction = new Reaction(generateRandomDouble());
model.setCurrentReaction(currentReaction);
timer.restart();
model.setRunning(true);
}
}
}
private double generateRandomDouble() {
double x1 = 10.0;
double x2 = 30.0;
double f = Math.random() / Math.nextDown(1.0);
return x1 * (1.0 - f) + x2 * f;
}
}
public class TimerListener implements ActionListener {
private final ReactionTestingGUI view;
private final ReactionTestingModel model;
public TimerListener(ReactionTestingGUI view, ReactionTestingModel model) {
this.view = view;
this.model = model;
}
@Override
public void actionPerformed(ActionEvent event) {
Timer timer = (Timer) event.getSource();
int delay = timer.getDelay();
double increment = 0.001 * delay;
model.getCurrentReaction().incrementActualTime(increment);
view.repaint();
}
}
public class ReactionTableModel extends DefaultTableModel {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
public class ReactionTestingModel {
private boolean running;
private final List<Reaction> reactions;
private Reaction currentReaction;
public ReactionTestingModel() {
this.reactions = new ArrayList<>();
this.currentReaction = new Reaction(0.0);
this.running = false;
}
public void addReaction(Reaction reaction) {
this.reactions.add(reaction);
}
public List<Reaction> getReactions() {
return reactions;
}
public Reaction getCurrentReaction() {
return currentReaction;
}
public void setCurrentReaction(Reaction currentReaction) {
this.currentReaction = currentReaction;
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
}
public class Reaction {
private double actualTime;
private final double expectedTime;
public Reaction(double expectedTime) {
this.expectedTime = expectedTime;
this.actualTime = 0.0;
}
public double getExpectedTime() {
return expectedTime;
}
public double getActualTime() {
return actualTime;
}
public void setActualTime(double actualTime) {
this.actualTime = actualTime;
}
public void incrementActualTime(double increment) {
this.actualTime += increment;
}
public double getAbsoluteDifference() {
return Math.abs(expectedTime - actualTime);
}
}
}