Advertisement
Guest User

Java auto clicker

a guest
Feb 24th, 2014
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. package core.ui;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.JButton;
  8. import javax.swing.JCheckBoxMenuItem;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JMenu;
  12. import javax.swing.JMenuBar;
  13. import javax.swing.JMenuItem;
  14. import javax.swing.JPanel;
  15. import javax.swing.JSpinner;
  16. import javax.swing.SpinnerNumberModel;
  17. import javax.swing.border.EmptyBorder;
  18. import javax.swing.event.ChangeEvent;
  19. import javax.swing.event.ChangeListener;
  20.  
  21. import org.jvnet.substance.SubstanceLookAndFeel;
  22. import org.jvnet.substance.api.skin.GraphiteAquaSkin;
  23.  
  24. import core.mouse.Mouse;
  25. import javax.swing.ImageIcon;
  26. import java.awt.Font;
  27.  
  28. public class Gui extends JFrame {
  29.  
  30.  
  31.     /*
  32.     *Written By TheScrub
  33.         *Under copyleft licensing.
  34.     */
  35.  
  36.     private JPanel contentPane;
  37.     private Mouse mouse = new Mouse();
  38.     private boolean useDelay = true;
  39.     private boolean clickMouse = false;
  40.  
  41.     public static void main(String[] args) {
  42.         EventQueue.invokeLater(new Runnable() {
  43.             public void run() {
  44.                 try {
  45.                     SubstanceLookAndFeel.setSkin(new GraphiteAquaSkin());
  46.                     Gui frame = new Gui();
  47.                     frame.setVisible(true);
  48.                 } catch (Exception e) {
  49.                     e.printStackTrace();
  50.                 }
  51.             }
  52.         });
  53.     }
  54.  
  55.     /**
  56.      * Create the frame.
  57.      */
  58.     public Gui() {
  59.         setTitle("Scrub Auto Clicker");
  60.         setAlwaysOnTop(true);
  61.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.         setBounds(100, 100, 207, 122);
  63.  
  64.         JMenuBar menuBar = new JMenuBar();
  65.         setJMenuBar(menuBar);
  66.  
  67.         JMenu mnFile = new JMenu("File");
  68.         mnFile.setFont(new Font("Calibri", Font.BOLD, 16));
  69.         mnFile.setIcon(new ImageIcon(Gui.class.getResource("/resource/application_view_detail.png")));
  70.         menuBar.add(mnFile);
  71.  
  72.         JMenuItem mntmAbout = new JMenuItem("About");
  73.         mntmAbout.addActionListener(new ActionListener() {
  74.             public void actionPerformed(ActionEvent arg0) {
  75.                 Information.main(null);
  76.  
  77.             }
  78.         });
  79.  
  80.         final JCheckBoxMenuItem chckbxmntmUseDelay = new JCheckBoxMenuItem(
  81.                 "Use Delay");
  82.         chckbxmntmUseDelay.setSelected(true);
  83.         mnFile.add(chckbxmntmUseDelay);
  84.         mnFile.add(mntmAbout);
  85.         contentPane = new JPanel();
  86.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  87.         setContentPane(contentPane);
  88.         contentPane.setLayout(null);
  89.  
  90.         final JLabel lblDelay = new JLabel("Delay: \r\n" + "1000 M/S");
  91.         lblDelay.setBounds(10, 11, 108, 14);
  92.         contentPane.add(lblDelay);
  93.  
  94.         final JButton btnStart = new JButton("Start");
  95.  
  96.         btnStart.setBounds(49, 36, 91, 23);
  97.         contentPane.add(btnStart);
  98.  
  99.         final JSpinner delaySpinner = new JSpinner();
  100.         delaySpinner.addChangeListener(new ChangeListener() {
  101.             public void stateChanged(ChangeEvent arg0) {
  102.                 lblDelay.setText("Delay: \r\n" + delaySpinner.getValue()
  103.                         + " M/S");
  104.             }
  105.         });
  106.         delaySpinner.setModel(new SpinnerNumberModel(1000, 1, 10000, 100));
  107.         delaySpinner.setBounds(128, 9, 61, 18);
  108.         contentPane.add(delaySpinner);
  109.  
  110.         btnStart.addActionListener(new ActionListener() {
  111.             public void actionPerformed(ActionEvent e) {
  112.                 clickMouse = !clickMouse ? true : false;
  113.                 System.out.println("Mouse clicking: " + clickMouse);
  114.                 mouse.setDelayTime((int) delaySpinner.getValue());
  115.                 mouse.setDelayUse(useDelay);
  116.                 mouse.setRunning(clickMouse);
  117.                 btnStart.setText(clickMouse ? "Stop" : "Start");
  118.                 if (clickMouse) {
  119.                     // creating new instance so we can call start more than once
  120.                     new Thread(mouse).start();
  121.                 }
  122.             }
  123.         });
  124.        
  125.         chckbxmntmUseDelay.addActionListener(new ActionListener() {
  126.             public void actionPerformed(ActionEvent e) {
  127.                 useDelay = chckbxmntmUseDelay.isSelected() ? true : false;
  128.                 delaySpinner.setEnabled(useDelay);
  129.             }
  130.         });
  131.     }
  132. }
  133.  
  134.  
  135. package core.mouse;
  136.  
  137. import java.awt.AWTException;
  138. import java.awt.MouseInfo;
  139. import java.awt.Point;
  140. import java.awt.Robot;
  141. import java.awt.event.InputEvent;
  142.  
  143.  
  144. public class Mouse extends Thread{
  145.  
  146.     private Robot robot;
  147.     private int delayTime;
  148.     private boolean useDelay;
  149.     private boolean running = false;
  150.  
  151.     public Mouse() {
  152.         try {
  153.             robot = new Robot();
  154.         } catch (AWTException e) {
  155.             System.out.println("Failed to load robot");
  156.             e.printStackTrace();
  157.         }
  158.     }
  159.  
  160.     public Point getLocation() {
  161.         return MouseInfo.getPointerInfo().getLocation();
  162.     }
  163.  
  164.     public String getLocationString() {
  165.         Point p = getLocation();
  166.         return p.x + "," + p.y;
  167.     }
  168.  
  169.     public void setDelayTime(int delayTime) {
  170.         this.delayTime = delayTime;
  171.     }
  172.  
  173.     public void setDelayUse(boolean useDelay) {
  174.         this.useDelay = useDelay;
  175.     }
  176.  
  177.     public void setRunning(boolean running) {
  178.         this.running = running;
  179.     }
  180.  
  181.     public boolean isRunning() {
  182.         return running;
  183.     }
  184.  
  185.     public void leftClickMouse() {
  186.         if (useDelay)
  187.             robot.delay(delayTime);
  188.         robot.mousePress(InputEvent.BUTTON1_MASK);
  189.         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  190.     }
  191.  
  192.     @Override
  193.     public void run(){
  194.         while (isRunning()) {
  195.             leftClickMouse();
  196.         }
  197.     }
  198.  
  199. }
  200. package core.ui;
  201.  
  202. import java.awt.BorderLayout;
  203.  
  204. public class Information extends JFrame {
  205.  
  206.     private JPanel contentPane;
  207.  
  208.     /**
  209.      * Launch the application.
  210.      */
  211.     public static void main(String[] args) {
  212.         EventQueue.invokeLater(new Runnable() {
  213.             public void run() {
  214.                 try {
  215.                     Information frame = new Information();
  216.                     frame.setVisible(true);
  217.                 } catch (Exception e) {
  218.                     e.printStackTrace();
  219.                 }
  220.             }
  221.         });
  222.     }
  223.  
  224.     /**
  225.      * Create the frame.
  226.      */
  227.     public Information() {
  228.         setTitle("Information");
  229.         setAlwaysOnTop(true);
  230.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  231.         setBounds(100, 100, 301, 167);
  232.         contentPane = new JPanel();
  233.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  234.         setContentPane(contentPane);
  235.         contentPane.setLayout(null);
  236.        
  237.         JEditorPane dtrpnThisAutoClicker = new JEditorPane();
  238.         dtrpnThisAutoClicker.setText("This auto clicker is for learning resources only \r\nyou can find a link to the source in here soon");
  239.         dtrpnThisAutoClicker.setEditable(false);
  240.         dtrpnThisAutoClicker.setBounds(10, 11, 273, 118);
  241.         contentPane.add(dtrpnThisAutoClicker);
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement