-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
120 lines (100 loc) · 3.77 KB
/
GUI.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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GUI implements ActionListener {
private int count = 0;
private int totalClicks = 0;
private long totalTime = 0;
private JLabel label;
private JLabel cpsLabel;
private JLabel timeLabel;
private JFrame frame;
private JButton button;
private JPanel panel;
private Date lastClickTime;
public GUI() {
frame = new JFrame();
button = new JButton("Click me");
button.addActionListener(this);
label = new JLabel("Number of clicks: 0");
cpsLabel = new JLabel("CPS: 0.0");
timeLabel = new JLabel("Time Between Clicks: N/A");
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(label);
panel.add(cpsLabel);
panel.add(timeLabel);
panel.add(button);
;
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Our GUI");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
count++;
totalClicks++;
if (lastClickTime != null) {
long timeDifference = new Date().getTime() - lastClickTime.getTime();
double cps = 1000.0 / timeDifference; // Clicks per second
totalTime += timeDifference;
//label
cpsLabel.setText(String.format("CPS: %.2f", cps));
timeLabel.setText("Time Between clicks: " + formatTimeDifference(timeDifference));
}
lastClickTime = new Date();
label.setText("Number of clicks: " + count);
// //Add the average click rate at each checkpoint
if (count == 50 || count == 75 || count == 100 || count == 200) {
double averageCPS = totalClicks / (totalTime / 1000.0); // Average Clicks per second
String message = String.format("Number of clicks: %d\nCPS: %.2f\nTime Between clicks: %s\nAverage CPS: %.2f",
count, Double.parseDouble(cpsLabel.getText().substring(5)), timeLabel.getText(), averageCPS);
JOptionPane.showMessageDialog(frame, message);
}
if (count == 65) {
JOptionPane.showMessageDialog(frame, "Seriously, You're still going?");
}
if (count == 80) {
JOptionPane.showMessageDialog(frame, "You can stop now");
}
if (count == 150) {
JOptionPane.showMessageDialog(frame, "Are you okay?");
}
if (count == 250) {
JOptionPane.showMessageDialog(frame, "Want to see a magic trick?");
}
if (count == 300) {
double averageCPS = totalClicks / (totalTime / 1000.0); // Average Clicks per second
String message = String.format(
"Number of clicks: %d\nCPS: %.2f\nTime Between clicks: %s\nAverage CPS: %.2f", count,
Double.parseDouble(cpsLabel.getText().substring(5)), timeLabel.getText(), averageCPS);
JOptionPane.showMessageDialog(frame, message);
// Reset count and related variables after reaching 300 clicks
count = 0;
totalClicks = 0;
totalTime = 0;
cpsLabel.setText("CPS: 0.0");
timeLabel.setText("Time Between Clicks: N/A");
}
}
private String formatTimeDifference(long timeInMillis) {
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:SSS");
return sdf.format(new Date(timeInMillis));
}
}