diff --git a/src/main/java/wtf/beatrice/autosqueal/Main.java b/src/main/java/wtf/beatrice/autosqueal/Main.java index 293bdaf..dc5598f 100644 --- a/src/main/java/wtf/beatrice/autosqueal/Main.java +++ b/src/main/java/wtf/beatrice/autosqueal/Main.java @@ -4,69 +4,28 @@ import com.github.kwhat.jnativehook.GlobalScreen; import com.github.kwhat.jnativehook.NativeHookException; import org.apache.logging.log4j.LogManager; 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.event.InputEvent; -import java.security.SecureRandom; -import java.util.Random; import java.util.Timer; public class Main { private static final int LOOPS_BEFORE_CLICK = 5; - private static final int TIME_BETWEEN_MOVES = 5; private static boolean running = true; 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!"); registerJNativeHook(); - Timer timer = new Timer(); - CursorMoveListener cursorMoveListener = new CursorMoveListener(); - 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); - - } + MainWindow mainWindow = new MainWindow(); + mainWindow.init(); } 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); - } } diff --git a/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java new file mode 100644 index 0000000..748f0b5 --- /dev/null +++ b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java @@ -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); + } +} diff --git a/src/main/java/wtf/beatrice/autosqueal/CursorMover.java b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java similarity index 89% rename from src/main/java/wtf/beatrice/autosqueal/CursorMover.java rename to src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java index 5b18470..e643f28 100644 --- a/src/main/java/wtf/beatrice/autosqueal/CursorMover.java +++ b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java @@ -1,4 +1,4 @@ -package wtf.beatrice.autosqueal; +package wtf.beatrice.autosqueal.controls; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -6,9 +6,9 @@ import org.apache.logging.log4j.Logger; import java.awt.*; 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 destY; @@ -22,7 +22,7 @@ public class CursorMover extends TimerTask { boolean isRunning = true; - public CursorMover(int destinationX, int destinationY) { + public SingleStepMovementTask(int destinationX, int destinationY) { currentX = MouseInfo.getPointerInfo().getLocation().x; currentY = MouseInfo.getPointerInfo().getLocation().y; diff --git a/src/main/java/wtf/beatrice/autosqueal/CursorMoveListener.java b/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java similarity index 96% rename from src/main/java/wtf/beatrice/autosqueal/CursorMoveListener.java rename to src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java index c2aec6c..af8b599 100644 --- a/src/main/java/wtf/beatrice/autosqueal/CursorMoveListener.java +++ b/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java @@ -1,4 +1,4 @@ -package wtf.beatrice.autosqueal; +package wtf.beatrice.autosqueal.listener; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/wtf/beatrice/autosqueal/KeyPressListener.java b/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java similarity index 97% rename from src/main/java/wtf/beatrice/autosqueal/KeyPressListener.java rename to src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java index 0570407..623baca 100644 --- a/src/main/java/wtf/beatrice/autosqueal/KeyPressListener.java +++ b/src/main/java/wtf/beatrice/autosqueal/listener/KeyPressListener.java @@ -1,4 +1,4 @@ -package wtf.beatrice.autosqueal; +package wtf.beatrice.autosqueal.listener; import com.github.kwhat.jnativehook.GlobalScreen; import com.github.kwhat.jnativehook.NativeHookException; diff --git a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java new file mode 100644 index 0000000..02c4bbf --- /dev/null +++ b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java @@ -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"); + } + + } +} diff --git a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java new file mode 100644 index 0000000..74090a5 --- /dev/null +++ b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java @@ -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; + + +}