Skip to content

Instantly share code, notes, and snippets.

@rucko24
Last active May 6, 2022 08:08
Show Gist options
  • Save rucko24/20bf026b189dd054130c323bae3722f9 to your computer and use it in GitHub Desktop.
Save rucko24/20bf026b189dd054130c323bae3722f9 to your computer and use it in GitHub Desktop.
Simple Robot
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author rubn
*/
public class SimpleRobot extends JFrame {
private static final ScheduledExecutorService SCHEDULED = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> scheduledFuture;
private final JButton initButton = new JButton("Init");
private final JButton stopButton = new JButton("Stop");
private final JPanel jPanel = new JPanel();
private static final int HEIGHT = 50;
private static final int WIDTH = 250;
private static final int TWO = 2;
private static final int DELAY = 5000;
public SimpleRobot() {
this.initComponents();
}
private void initComponents() {
this.initButton();
this.stopButton();
this.jPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
jPanel.setBorder(BorderFactory.createEtchedBorder());
super.setAlwaysOnTop(true);
super.add(this.jPanel);
super.setTitle("B0t Click v1.0");
super.setSize(WIDTH, HEIGHT);
super.setResizable(true);
super.pack();
super.setLocation(0,0);
super.setVisible(true);
}
private void initButton() {
Robot robot = null;
final AtomicReference<Robot> robotAtomicReference = new AtomicReference<>();
try {
robot = new Robot();
robotAtomicReference.set(robot);
} catch (AWTException e) {
e.printStackTrace();
}
initButton.setFocusPainted(false);
initButton.addActionListener(e -> {
this.initButton.setEnabled(false);
this.stopButton.setEnabled(true);
this.scheduledFuture = SCHEDULED.scheduleAtFixedRate(() -> {
final int[] center = this.center();
robotAtomicReference.get().mouseMove(center[0], center[1]);
robotAtomicReference.get().mousePress(InputEvent.BUTTON1_DOWN_MASK);
robotAtomicReference.get().mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robotAtomicReference.get().delay(DELAY);
final int[] leftMiddleCenter = this.leftMiddleCenter();
robotAtomicReference.get().mouseMove(0, leftMiddleCenter[0]);
robotAtomicReference.get().mousePress(InputEvent.BUTTON1_DOWN_MASK);
robotAtomicReference.get().mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robotAtomicReference.get().delay(DELAY);
}, 0, DELAY, TimeUnit.MILLISECONDS);
});
this.jPanel.add(initButton);
}
private void stopButton() {
stopButton.setFocusPainted(false);
stopButton.addActionListener(e -> {
this.initButton.setEnabled(true);
this.stopButton.setEnabled(false);
if (Objects.nonNull(this.scheduledFuture)) {
scheduledFuture.cancel(true);
}
});
this.jPanel.add(this.stopButton);
}
private int[] center() {
final int ancho = Toolkit.getDefaultToolkit().getScreenSize().width / TWO;
final int altura = Toolkit.getDefaultToolkit().getScreenSize().height / TWO;
return new int[]{ancho, altura};
}
private int[] leftMiddleCenter() {
return new int[]{Toolkit.getDefaultToolkit().getScreenSize().height / TWO};
}
public static void main(String... agrs) {
try {
UIManager.setLookAndFeel(UIManager.getLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}
new Thread(SimpleRobot::new).start();
}
}
@rucko24
Copy link
Author

rucko24 commented Jun 16, 2021

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment