The image behind contains
I needed an image, ok?
Why does this exist? Well, I'm providing the source code as a download, so there is nothing stopping anyone from
opening it
and looking inside.
Might as well explain myself at that point.
Lines numbers in the code blocks on this website are almost the same as in flickspeed.java
.
flickspeed is based on the Java Swing GUI framework. I will cover the main logic of the program, then delve
into
framework-specific methods and workings.
data.dat
file in the same directory as the program.
This file contains 4 integer values and 1 boolean value for specific configurations in the program. If this file
does not exist in the directory that the program is in, the program setup will run.
data.dat
file:
400
0.5
2
1
true
data.dat
file.
data.dat
file and store them
into
global variables, namely:
static double DPI;
static double NOTIFY_SPEED;
static int NOTIFY_TIMER;
static int CONVERSION;
static boolean PERSISTENT_OVERLAY;
DPI
is the inputted mouse dpi, used for calculation of speed.
NOTIFY_SPEED
is the speed which is needed to trigger the flick! window appearing.
NOTIFY_TIMER
is the amount of time that the flick! notification pauses the thread and stays
on-screen.
CONVERSION
is a constant that dictates the type of conversion that is performed on the raw speed
taken from the display.
PERSISTENT_OVERLAY
dictates whether the persistent overlay starts when launching the program.
public static double calculateSpeed(double dpi) {
Point p = MouseInfo.getPointerInfo().getLocation();
try {
TimeUnit.MILLISECONDS.sleep(200);
}
catch (InterruptedException e) { Thread.currentThread().interrupt();}
Point n = MouseInfo.getPointerInfo().getLocation();
return Math.abs(n.distance(p));
}
java.io.File
object is created on launch to check for existence of a file.
File f = new File("data.dat");
if (f.exists() && !f.isDirectory()) {}
java.util.Scanner
object is created to scan the file's contents.
Scanner fileScan = new Scanner(f);
String line;
for (int i = 1; i < 6; i++) {
// Scan lines
}
switch()
and casted to their various types.
java.nio.file.Path
object is created as a placeholder for the
predetermined
path of the new file. It is then placed into the static method java.nio.file.Files.write()
, which
will
write the data collected to a file. After this, the program is terminated.
Path file = Paths.get("data.dat");
Files.write(file, lines);
final static Color COLOR_BG = new Color(22, 32, 53, 255);
final static Font DEFAULT_FONT = new Font(FONT_NAME, Font.PLAIN, 16);
final static Font DEFAULT_FONT_SMALL = new Font(FONT_NAME, Font.PLAIN, 13);
final static Font DEFAULT_TITLE = new Font(FONT_NAME, Font.BOLD, 22);
javax.swing.JFrame
objects.
data.dat
file or run straight from the tray menu, a
JFrame
independent of the other JFrames is created. It is given a set of properties and a
java.awt.LayoutManager
to organize the elements in the frame.
JFrame firstRunFrame = new JFrame("flickspeed Setup");
firstRunFrame.setIconImage(new ImageIcon("res/logo.png").getImage());
firstRunFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
firstRunFrame.getContentPane().setMaximumSize(new Dimension(344, 379));
firstRunFrame.setResizable(false);
firstRunFrame.getContentPane().setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridLayout(0, 1));
javax.swing.JPanel
is created inside the frame for easier management, and then all UI elements
are defined and
given properties.
JLabel insertDPI = new JLabel("Mouse DPI:");
insertDPI.setForeground(Color.white);
insertDPI.setFont(DEFAULT_FONT);
JTextField dpiField = new JTextField();
dpiField.setBackground(COLOR_BG);
dpiField.setForeground(Color.white);
dpiField.setFont(DEFAULT_FONT_SMALL);
if (time.matches("\\d+")) {
JLabel errorLabel1 = new JLabel("Please enter a valid number.");
errorLabel1.setVisible(false);
errorLabel1.setForeground(Color.red);
errorLabel1.setFont(DEFAULT_FONT_SMALL);
else { errorLabel1.setVisible(true);}
JLabel spLb = null;
if (PERSISTENT_OVERLAY) {
spLb = createPersistentOverlay();
updatePersistentOverlay(spLb, speed);
public static void updatePersistentOverlay(JLabel label, double speed) {
String labelText = String.format("%.2f", speed);
labelText += UNIT;
label.setText(labelText);
}
javax.swing.SwingUtilities.getRoot()
, which returns the JFrame that
the provided object is in.
Creation
spLb = createPersistentOverlay();
public static JLabel createPersistentOverlay() {
JFrame frame = new JFrame("flickspeed Persistent Overlay");
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.setFocusableWindowState(false);
frame.setAutoRequestFocus(false);
frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false);
Deletion
JFrame frame = (JFrame) SwingUtilities.getRoot(spLb);
frame.dispose();
if (speed >= NOTIFY_SPEED) {
createFrame(speed);
}
public static void createFrame(double speed) {
JFrame frame = new JFrame("flickspeed");
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.setFocusableWindowState(false);
frame.setAutoRequestFocus(false);
frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false);
frame.pack();
frame.transferFocusBackward();
try {
TimeUnit.SECONDS.sleep(NOTIFY_TIMER);
}
catch(InterruptedException e) { Thread.currentThread().interrupt();}
frame.setVisible(false);
frame.dispose();