Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 4cfa15e

Browse files
committed
Added graph showing recorded keystrokes. Clamps the min/max times.
1 parent 9e70dbd commit 4cfa15e

5 files changed

Lines changed: 98 additions & 6 deletions

File tree

Lang.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
// Changelog
2828
401:Initial release
2929
402:Fixed issue where the statistics key-interceptor would never be destroyed.\n\nRefactored code and added documentation to a few more classes.
30-
403:Added start argument to inject into other JVM processes.\n\nAdded start argument to change settings directory.\n\nSliders now show their numerical value.\n\nFixed content on the Home tab not being padded making it harder to read.\n\nFixed content on the Home tab having an ugly border.\n\nFixed statistic data not being padded making it harder to read.\n\nFixed combo-box going off the page.\n\nMisc. tiny code changes.
30+
403:Added start argument to inject into other JVM processes.\n\nAdded start argument to change settings directory.\n\nAdded a basic graph display to statistics.\n\nSliders now show their numerical value.\n\nFixed content on the Home tab not being padded making it harder to read.\n\nFixed content on the Home tab having an ugly border.\n\nFixed statistic data not being padded making it harder to read.\n\nFixed combo-box going off the page.\n\nMisc. tiny code changes.

src/me/coley/clicker/Stats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void onEnable() {
3131
MainGUI.log.log(Level.INFO, "Beginning recording of mouse input.");
3232
frequency = new DescriptiveStatistics();
3333
MainGUI.log.log(Level.INFO, "Creating keybind-listener...");
34-
mouseHook = new StatRecorder(this);
34+
mouseHook = new StatRecorder(this, gui.graph);
3535
MouseHook.hook(mouseHook);
3636
}
3737

src/me/coley/clicker/jna/StatRecorder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package me.coley.clicker.jna;
22

33
import me.coley.clicker.Stats;
4+
import me.coley.clicker.ui.GraphingPanel;
45
import me.coley.simplejna.hook.mouse.MouseEventReceiver;
56
import me.coley.simplejna.hook.mouse.struct.MOUSEHOOKSTRUCT;
67
import me.coley.simplejna.hook.mouse.struct.MouseButtonType;
78

89
public class StatRecorder extends MouseEventReceiver {
910
private long last = -1;
1011
private final Stats stats;
12+
private final GraphingPanel graph;
1113

12-
public StatRecorder(Stats stats) {
14+
public StatRecorder(Stats stats, GraphingPanel graph) {
1315
this.stats = stats;
16+
this.graph = graph;
1417
}
1518

1619
@Override
1720
public boolean onMousePress(MouseButtonType type, MOUSEHOOKSTRUCT info) {
1821
long now = System.currentTimeMillis();
1922
if (last != -1) {
2023
stats.getFrequencyData().addValue(now - last);
24+
graph.addValue((int) (now-last));
2125
}
2226
last = now;
2327
return false;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package me.coley.clicker.ui;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics;
5+
import java.awt.Graphics2D;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import javax.swing.JPanel;
10+
11+
public class GraphingPanel extends JPanel {
12+
private final static int MAX_STAT = 1000;
13+
private final static int SIZE_CUT = 10;
14+
private final static int CAPACITY = MAX_STAT / SIZE_CUT;
15+
private ArrayList<Integer> data = new ArrayList<Integer>(CAPACITY);
16+
private double max = 0;
17+
18+
public GraphingPanel() {
19+
clear();
20+
}
21+
22+
@Override
23+
public void paintComponent(Graphics gg) {
24+
super.paintComponent(gg);
25+
Graphics2D g = (Graphics2D) gg;
26+
int w = getWidth();
27+
int h = getHeight();
28+
29+
g.setColor(getBackground());
30+
g.clearRect(0, 0, w, h);
31+
g.setColor(Color.black);
32+
33+
int minY = 0, maxY = 1;
34+
for (int i = 0; i < data.size(); i++) {
35+
int val = data.get(i);
36+
if (val > 0) {
37+
if (minY == 0) {
38+
minY = i;
39+
maxY = i + 1;
40+
}
41+
else {
42+
maxY = i;
43+
}
44+
}
45+
}
46+
for (int i = minY; i < maxY; i++) {
47+
int val = data.get(i);
48+
double he = val == 0 ? 0 : val / max;
49+
int rw = w / (maxY - minY);
50+
int rx = (i-minY) * rw;
51+
int rh = (int) Math.round((he) * h / 1.3);
52+
int ry = h - rh;
53+
g.fillRect(rx, ry, rw, rh);
54+
}
55+
}
56+
57+
public void clear() {
58+
max = 0;
59+
while (data.size() <= CAPACITY) {
60+
data.add(0);
61+
}
62+
for (int i = 0; i < data.size(); i++) {
63+
data.set(i, 0);
64+
}
65+
}
66+
67+
public void addValue(int i) {
68+
if (i > MAX_STAT) {
69+
i = MAX_STAT;
70+
}
71+
i /= SIZE_CUT;
72+
//
73+
int val = data.get(i);
74+
if (val + 1 > max) {
75+
max = val + 1;
76+
}
77+
data.set(i, val + 1);
78+
repaint();
79+
}
80+
}

src/me/coley/clicker/ui/MainGUI.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
import me.coley.simplejna.hook.key.KeyHook;
4040

4141
import java.awt.BorderLayout;
42+
import java.awt.Color;
4243
import java.awt.Dimension;
4344

4445
import javax.swing.JPanel;
46+
import javax.swing.JScrollPane;
4547

4648
import java.util.Random;
4749
import java.util.logging.Level;
@@ -57,6 +59,7 @@ public class MainGUI {
5759
public final Clicker clicker = new Clicker(this);
5860
public final Stats stats = new Stats(this);
5961
private final KeyHandler handler = new KeyHandler(this);
62+
public GraphingPanel graph;
6063
private JFrame frmClicker;
6164
private JTextArea txtStats;
6265

@@ -194,16 +197,22 @@ public void itemStateChanged(ItemEvent e) {
194197

195198
JPanel tabStatistics = new JPanel();
196199
tabs.addTab(Lang.get(Lang.STATISTICS_TITLE), null, tabStatistics, null);
197-
tabStatistics.setLayout(new BorderLayout(0, 0));
200+
tabStatistics.setLayout(new BorderLayout());
198201
{
202+
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
203+
graph = new GraphingPanel();
199204
txtStats = new JTextArea();
200205
txtStats.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
201206
txtStats.setEditable(false);
202207
txtStats.setFocusable(false);
203208
txtStats.setText(Lang.get(Lang.STATISTICS_RECORD_SOME_DATA));
204209
txtStats.setBackground(SystemColor.control);
205210
txtStats.setFont(new Font("Arial", Font.PLAIN, 12));
206-
tabStatistics.add(txtStats, BorderLayout.CENTER);
211+
212+
sp.setTopComponent(graph);
213+
sp.setBottomComponent(new JScrollPane(txtStats));
214+
sp.setDividerLocation(frmClicker.getHeight() / 2);
215+
tabStatistics.add(sp, BorderLayout.CENTER);
207216
}
208217
log.log(Level.INFO, "Displaying gui");
209218
frmClicker.setVisible(true);
@@ -277,5 +286,4 @@ public void onRecordingFinished() {
277286
public void toggleVisible() {
278287
frmClicker.setVisible(!frmClicker.isVisible());
279288
}
280-
281289
}

0 commit comments

Comments
 (0)