Source code to AutoClicker


SUBMITTED BY: tigercn

DATE: Jan. 7, 2017, 4:07 p.m.

FORMAT: Text only

SIZE: 5.6 kB

HITS: 571

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class Frame extends JFrame {
  5. private static final long serialVersionUID = 1L;
  6. private JPanel container;
  7. private JPanel ratePanel, delayPanel, south, numberPanel;
  8. private JButton button1, button2;
  9. private JTextField delayText, rateText, numberText;
  10. private JLabel delayLabel, rateLabel, numberLabel;
  11. private boolean autoclick = false;
  12. private Thread t;
  13. public Frame() {
  14. this.setTitle("Autoclick");
  15. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. this.setSize(250, 320);
  17. this.setLocationRelativeTo(null);
  18. this.setResizable(false);
  19. this.setAlwaysOnTop(true);
  20. initComposant();
  21. this.setContentPane(container);
  22. this.setVisible(true);
  23. }
  24. private void initComposant() {
  25. // Delay
  26. delayPanel = new JPanel();
  27. delayPanel.setBorder(BorderFactory.createTitledBorder("Delay"));
  28. delayPanel.setPreferredSize(new Dimension(150, 75));
  29. delayText = new JTextField();
  30. delayText.setPreferredSize(new Dimension(50, 25));
  31. delayLabel = new JLabel("Delay in millisecodns");
  32. delayPanel.add(delayText);
  33. delayPanel.add(delayLabel);
  34. // Rate
  35. ratePanel = new JPanel();
  36. ratePanel.setPreferredSize(new Dimension(150, 75));
  37. ratePanel.setBorder(BorderFactory.createTitledBorder("Rate"));
  38. rateText = new JTextField();
  39. rateText.setPreferredSize(new Dimension(50, 25));
  40. rateLabel = new JLabel("Rate in milliseconds");
  41. ratePanel.add(rateLabel);
  42. ratePanel.add(rateText);
  43. // Number
  44. numberPanel = new JPanel();
  45. numberPanel.setPreferredSize(new Dimension(150, 75));
  46. numberPanel.setBorder(BorderFactory.createTitledBorder("Number"));
  47. numberText = new JTextField();
  48. numberText.setPreferredSize(new Dimension(50, 25));
  49. numberLabel = new JLabel("Number of clicks");
  50. numberPanel.add(numberLabel);
  51. numberPanel.add(numberText);
  52. // Creation du JPanel south
  53. south = new JPanel();
  54. // Création du bouton1 et bouton2 et ajout au JPanel south
  55. this.button1 = new JButton("GO!");
  56. this.button2 = new JButton("STOP!");
  57. this.button1.setEnabled(true);
  58. this.button2.setEnabled(false);
  59. this.button1.addActionListener(new Button1Listener());
  60. this.button2.addActionListener(new Button2Listener());
  61. south.add(this.button1);
  62. south.add(this.button2);
  63. container = new JPanel();
  64. container.add(delayPanel);
  65. container.add(ratePanel);
  66. container.add(numberPanel);
  67. container.add(south, BorderLayout.SOUTH);
  68. }
  69. public void go() {
  70. long rate = Long.parseLong(rateText.getText());
  71. System.out.println(rate);
  72. long delay = Long.parseLong(delayText.getText());
  73. System.out.println(delay);
  74. int number = Integer.parseInt(numberText.getText());
  75. System.out.println(number);
  76. try {
  77. Thread.sleep(delay);
  78. } catch (InterruptedException e) {
  79. }
  80. for (int i = 0; i < number && autoclick; i++) {
  81. try {
  82. Robot robot = new Robot();
  83. try {
  84. Thread.sleep(rate);
  85. robot.mousePress(InputEvent.BUTTON1_MASK);
  86. robot.mouseRelease(InputEvent.BUTTON1_MASK);
  87. } catch (InterruptedException ex) {
  88. }
  89. } catch (AWTException e) {
  90. }
  91. }
  92. button2.doClick();
  93. }
  94. // Class qui écoute le 1er bouton
  95. class Button1Listener implements ActionListener {
  96. public void actionPerformed(ActionEvent arg0) {
  97. autoclick = true;
  98. t = new Thread(new AutoClick());
  99. t.start();
  100. button1.setEnabled(false);
  101. button2.setEnabled(true);
  102. }
  103. }
  104. // Class qui écoute le 2nd bouton
  105. class Button2Listener implements ActionListener {
  106. public void actionPerformed(ActionEvent arg0) {
  107. autoclick = false;
  108. button1.setEnabled(true);
  109. button2.setEnabled(false);
  110. }
  111. }
  112. class AutoClick implements Runnable {
  113. public void run() {
  114. go();
  115. }
  116. }
  117. public static void main(String[] args) {
  118. new Frame();
  119. }
  120. }

comments powered by Disqus