Advertisement
Guest User

AutoClicker

a guest
Mar 5th, 2015
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. package com.autoclicker;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Robot;
  5. import java.awt.Toolkit;
  6. import java.awt.event.InputEvent;
  7. import java.util.Random;
  8.  
  9. import javax.swing.JFrame;
  10.  
  11. public class AutoClicker extends javax.swing.JFrame {
  12.     private static final long serialVersionUID = 4035771675111011526L;
  13.  
  14.     private static int CLICK_INTERVAL = 40 * 1000; // 40 sec
  15.     // private static int CLICK_INTERVAL = 5000; // 5 sec
  16.     private Clicker clicker;
  17.  
  18.     public AutoClicker() {
  19.         initComponents();
  20.         setStartEnabled(true);
  21.         clicker = null;
  22.     }
  23.  
  24.     private void setStartEnabled(boolean state) {
  25.         startButton.setEnabled(state);
  26.         stopButton.setEnabled(!state);
  27.     }
  28.  
  29.     private void initComponents() {
  30.         startButton = new javax.swing.JButton();
  31.         stopButton = new javax.swing.JButton();
  32.  
  33.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  34.  
  35.         startButton.setText("Start");
  36.         startButton.addActionListener(new java.awt.event.ActionListener() {
  37.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  38.                 startButtonActionPerformed(evt);
  39.             }
  40.         });
  41.  
  42.         stopButton.setText("Stop");
  43.         stopButton.addActionListener(new java.awt.event.ActionListener() {
  44.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  45.                 stopButtonActionPerformed(evt);
  46.             }
  47.         });
  48.  
  49.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
  50.                 getContentPane());
  51.         getContentPane().setLayout(layout);
  52.         layout.setHorizontalGroup(layout.createParallelGroup(
  53.                 javax.swing.GroupLayout.Alignment.LEADING).addGroup(
  54.                 layout.createSequentialGroup()
  55.                         .addContainerGap()
  56.                         .addComponent(startButton,
  57.                                 javax.swing.GroupLayout.DEFAULT_SIZE, 250,
  58.                                 Short.MAX_VALUE)
  59.                         .addGap(28, 28, 28)
  60.                         .addComponent(stopButton,
  61.                                 javax.swing.GroupLayout.PREFERRED_SIZE, 239,
  62.                                 javax.swing.GroupLayout.PREFERRED_SIZE)
  63.                         .addContainerGap()));
  64.         layout.setVerticalGroup(layout
  65.                 .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  66.                 .addGroup(
  67.                         layout.createSequentialGroup()
  68.                                 .addContainerGap()
  69.                                 .addGroup(
  70.                                         layout.createParallelGroup(
  71.                                                 javax.swing.GroupLayout.Alignment.LEADING,
  72.                                                 false)
  73.                                                 .addComponent(
  74.                                                         stopButton,
  75.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  76.                                                         185, Short.MAX_VALUE)
  77.                                                 .addComponent(
  78.                                                         startButton,
  79.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  80.                                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  81.                                                         Short.MAX_VALUE))
  82.                                 .addContainerGap(
  83.                                         javax.swing.GroupLayout.DEFAULT_SIZE,
  84.                                         Short.MAX_VALUE)));
  85.  
  86.         pack();
  87.     }// </editor-fold>
  88.  
  89.     private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
  90.         startButton.setEnabled(false);
  91.         stopButton.setEnabled(false);
  92.  
  93.         clicker = new Clicker();
  94.         clicker.start();
  95.  
  96.         setStartEnabled(false);
  97.     }
  98.  
  99.     private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
  100.         startButton.setEnabled(false);
  101.         stopButton.setEnabled(false);
  102.         clicker.killThread();
  103.         clicker = null;
  104.         setStartEnabled(true);
  105.     }
  106.  
  107.     public static void main(String args[]) {
  108.         AutoClicker frame = new AutoClicker();
  109.         frame.setLocationRelativeTo(null);
  110.         frame.setResizable(false);
  111.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112.         frame.setVisible(true);
  113.     }
  114.  
  115.     // Variables declaration - do not modify
  116.     private javax.swing.JButton startButton;
  117.     private javax.swing.JButton stopButton;
  118.  
  119.     // End of variables declaration
  120.  
  121.     class Clicker extends Thread {
  122.         private Robot robot;
  123.         private Random random;
  124.         private int clickHeight;
  125.         private int clickWidth;
  126.         private boolean toContinue;
  127.         private int randomSlack;
  128.  
  129.         public Clicker() {
  130.             try {
  131.                 robot = new Robot();
  132.                 random = new Random();
  133.  
  134.                 Dimension screenSize = Toolkit.getDefaultToolkit()
  135.                         .getScreenSize();
  136.                 int screenHeight = (int) (screenSize.getHeight() + 1e-7);
  137.                 int screenWidth = (int) (screenSize.getWidth() + 1e-7);
  138.  
  139.                 int minSlack = Math.min(screenHeight, screenWidth);
  140.  
  141.                 randomSlack = minSlack / 8;
  142.  
  143.                 clickHeight = screenHeight / 2;
  144.                 clickWidth = screenWidth / 2;
  145.             } catch (Exception ex) {
  146.                 ex.printStackTrace();
  147.             }
  148.         }
  149.  
  150.         @Override
  151.         public void run() {
  152.             toContinue = true;
  153.  
  154.             while (toContinue) {
  155.                 try {
  156.                     Thread.sleep(CLICK_INTERVAL);
  157.  
  158.                     if (toContinue) {
  159.                         for (int i = 0; i < 3 && toContinue; i++) {
  160.                             robot.mouseMove(clickWidth + getRandomSlack(),
  161.                                     clickHeight + getRandomSlack());
  162.                             robot.mousePress(InputEvent.BUTTON1_MASK);
  163.                             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  164.                             Thread.sleep(100);
  165.                         }
  166.                         // System.out.println("Clicked");
  167.                     }
  168.                 } catch (InterruptedException ex) {
  169.                 }
  170.             }
  171.         }
  172.  
  173.         private int getRandomSlack() {
  174.             return random.nextInt(randomSlack) - (randomSlack / 2);
  175.         }
  176.  
  177.         public void killThread() {
  178.             toContinue = false;
  179.             this.interrupt();
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement