Refactor packages and add GUI with start button
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2024-08-05 20:39:14 +02:00
parent 9c0b3290f7
commit b038098c3c
7 changed files with 154 additions and 65 deletions

View File

@ -4,69 +4,28 @@ import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException; import com.github.kwhat.jnativehook.NativeHookException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.util.RunnerUtil;
import wtf.beatrice.autosqueal.listener.KeyPressListener;
import wtf.beatrice.autosqueal.ui.MainWindow;
import java.awt.*; import java.awt.*;
import java.awt.event.InputEvent;
import java.security.SecureRandom;
import java.util.Random;
import java.util.Timer; import java.util.Timer;
public class Main { public class Main {
private static final int LOOPS_BEFORE_CLICK = 5; private static final int LOOPS_BEFORE_CLICK = 5;
private static final int TIME_BETWEEN_MOVES = 5;
private static boolean running = true; private static boolean running = true;
private static final Logger LOGGER = LogManager.getLogger(Main.class); private static final Logger LOGGER = LogManager.getLogger(Main.class);
public static void main(String[] args) throws InterruptedException, AWTException { public static void main(String[] args) {
LOGGER.info("Hello world!"); LOGGER.info("Hello world!");
registerJNativeHook(); registerJNativeHook();
Timer timer = new Timer(); MainWindow mainWindow = new MainWindow();
CursorMoveListener cursorMoveListener = new CursorMoveListener(); mainWindow.init();
timer.schedule(cursorMoveListener, 0L, 1000L);
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
Random random = new SecureRandom();
while (running) {
//if(!cursorMoveListener.isUserAway()) continue;
for(int i = 0; i < LOOPS_BEFORE_CLICK; i++) {
int currentX = MouseInfo.getPointerInfo().getLocation().x;
int currentY = MouseInfo.getPointerInfo().getLocation().y;
LOGGER.info("Starting coordinates: {}, {}", currentX, currentY);
int randomX;
int randomY;
randomX = random.nextInt(width);
randomY = random.nextInt(height);
LOGGER.info("Destination coordinates: {}, {}", randomX, randomY);
startMover(randomX, randomY);
}
startMover(width - 5, 5);
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(500);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
}
} }
private static void registerJNativeHook() { private static void registerJNativeHook() {
@ -84,16 +43,4 @@ public class Main {
} }
} }
private static void startMover(int destX, int destY) throws InterruptedException {
Timer timer = new Timer();
CursorMover cursorMover = new CursorMover(destX, destY);
timer.schedule(cursorMover, 0L, 2L);
while (cursorMover.isRunning()) {
Thread.sleep(200);
}
timer.cancel();
Thread.sleep(TIME_BETWEEN_MOVES * 1000L);
}
} }

View File

@ -0,0 +1,45 @@
package wtf.beatrice.autosqueal.controls;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;
import java.security.SecureRandom;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class CursorMover extends TimerTask
{
private static final Logger LOGGER = LogManager.getLogger(CursorMover.class);
private final Random RANDOM;
private final int SCREEN_HEIGHT;
private final int SCREEN_WIDTH;
public CursorMover(int screenWidth, int screenHeight) {
RANDOM = new SecureRandom();
this.SCREEN_WIDTH = screenWidth;
this.SCREEN_HEIGHT = screenHeight;
}
@Override
public void run() {
int currentX = MouseInfo.getPointerInfo().getLocation().x;
int currentY = MouseInfo.getPointerInfo().getLocation().y;
LOGGER.info("Starting coordinates: {}, {}", currentX, currentY);
int destX = RANDOM.nextInt(SCREEN_WIDTH);
int destY = RANDOM.nextInt(SCREEN_HEIGHT);
LOGGER.info("Destination coordinates: {}, {}", destX, destY);
Timer timer = new Timer();
SingleStepMovementTask singleStepMovementTask = new SingleStepMovementTask(destX, destY);
timer.schedule(singleStepMovementTask, 0L, 2L);
}
}

View File

@ -1,4 +1,4 @@
package wtf.beatrice.autosqueal; package wtf.beatrice.autosqueal.controls;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -6,9 +6,9 @@ import org.apache.logging.log4j.Logger;
import java.awt.*; import java.awt.*;
import java.util.TimerTask; import java.util.TimerTask;
public class CursorMover extends TimerTask { public class SingleStepMovementTask extends TimerTask {
private static final Logger LOGGER = LogManager.getLogger(CursorMover.class); private static final Logger LOGGER = LogManager.getLogger(SingleStepMovementTask.class);
final int destX; final int destX;
final int destY; final int destY;
@ -22,7 +22,7 @@ public class CursorMover extends TimerTask {
boolean isRunning = true; boolean isRunning = true;
public CursorMover(int destinationX, int destinationY) { public SingleStepMovementTask(int destinationX, int destinationY) {
currentX = MouseInfo.getPointerInfo().getLocation().x; currentX = MouseInfo.getPointerInfo().getLocation().x;
currentY = MouseInfo.getPointerInfo().getLocation().y; currentY = MouseInfo.getPointerInfo().getLocation().y;

View File

@ -1,4 +1,4 @@
package wtf.beatrice.autosqueal; package wtf.beatrice.autosqueal.listener;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

View File

@ -1,4 +1,4 @@
package wtf.beatrice.autosqueal; package wtf.beatrice.autosqueal.listener;
import com.github.kwhat.jnativehook.GlobalScreen; import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException; import com.github.kwhat.jnativehook.NativeHookException;

View File

@ -0,0 +1,84 @@
package wtf.beatrice.autosqueal.ui;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.controls.CursorMover;
import wtf.beatrice.autosqueal.listener.CursorMoveListener;
import wtf.beatrice.autosqueal.util.RunnerUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
public class MainWindow
{
private static final Logger LOGGER = LogManager.getLogger(MainWindow.class);
private static final int WINDOW_HEIGHT = 600;
private static final int WINDOW_WIDTH = 800;
private final Timer TIMER_RUNNER = new Timer();
private CursorMover cursorMover = null;
public MainWindow() {
}
public void init() {
JFrame frame = new JFrame();
frame.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
Button toggleButton = new Button("Start");
toggleButton.setBounds(new Rectangle((WINDOW_WIDTH / 2) - 40, WINDOW_HEIGHT - 60, 80, 30));
toggleButton.addActionListener(e -> toggleRunning(toggleButton));
frame.add(toggleButton);
int bordersPx = 10;
int rescaleRateo = ((WINDOW_WIDTH - (2 * bordersPx)) * 100) / RunnerUtil.SCREEN_WIDTH;
int rescaleWidth = RunnerUtil.SCREEN_WIDTH * rescaleRateo / 100;
int rescaleHeight = RunnerUtil.SCREEN_HEIGHT * rescaleRateo / 100;
JLabel imageLabel = new JLabel(getScreenCapture(rescaleWidth, rescaleHeight));
imageLabel.setBounds(new Rectangle(bordersPx, bordersPx, rescaleWidth, rescaleHeight));
frame.add(imageLabel);
frame.setLayout(null);
frame.setVisible(true);
}
private ImageIcon getScreenCapture(int rescaleWidth, int rescaleHeight) {
try {
Robot robot = new Robot();
BufferedImage fullImage = robot.createScreenCapture(new Rectangle(RunnerUtil.SCREEN_WIDTH, RunnerUtil.SCREEN_HEIGHT));
Image image = fullImage.getScaledInstance(rescaleWidth, rescaleHeight, Image.SCALE_FAST);
return new ImageIcon(image);
} catch (AWTException e) {
LOGGER.error("Robot initialization error", e);
return null;
}
}
private void toggleRunning(Button button) {
if (cursorMover == null) {
CursorMoveListener cursorMoveListener = new CursorMoveListener();
TIMER_RUNNER.schedule(cursorMoveListener, 0L, 1000L);
cursorMover = new CursorMover(RunnerUtil.SCREEN_WIDTH, RunnerUtil.SCREEN_HEIGHT);
TIMER_RUNNER.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L);
button.setLabel("Stop");
}
else {
TIMER_RUNNER.cancel();
cursorMover = null;
button.setLabel("Start");
}
}
}

View File

@ -0,0 +1,13 @@
package wtf.beatrice.autosqueal.util;
import java.awt.*;
public class RunnerUtil {
public static final int SECONDS_BETWEEN_MOVES = 5;
public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
public static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
}