Advertisement
Crenox

Winston Tutorial 11 & 12 Mouse Mover

Jul 27th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package com.samkough.main;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. @SuppressWarnings("serial")
  8. public class Win extends JFrame implements ActionListener
  9. {
  10. // private static final int WIDTH = 300;
  11. // private static final int HEIGHT = 300;
  12.  
  13. JLabel l1, l2, l3, l4, errorLabel;
  14. JTextField tf1, tf2, tf3, tf4;
  15. JButton b1;
  16.  
  17. public Win()
  18. {
  19. // rows, columns, then the last two are for padding
  20. // horizontal padding and vertical padding
  21. setLayout(new GridLayout(5, 2, 5, 5));
  22.  
  23. l1 = new JLabel("Enter number of movements:");
  24. l2 = new JLabel("Enter delay between movements milliseconds:");
  25. l3 = new JLabel("Enter screen width in pixels:");
  26. l4 = new JLabel("Enter screen height in pixels:");
  27. errorLabel = new JLabel("");
  28. tf1 = new JTextField();
  29. tf2 = new JTextField();
  30. tf3 = new JTextField();
  31. tf4 = new JTextField();
  32. b1 = new JButton("Start!");
  33.  
  34. add(l1);
  35. add(tf1);
  36. add(l2);
  37. add(tf2);
  38. add(l3);
  39. add(tf3);
  40. add(l4);
  41. add(tf4);
  42. add(b1);
  43. add(errorLabel);
  44.  
  45. b1.addActionListener(this);
  46. }
  47.  
  48. public void actionPerformed(ActionEvent e)
  49. {
  50. try
  51. {
  52. int num = (int)(Double.parseDouble(tf1.getText()));
  53. int delay = (int)(Double.parseDouble(tf2.getText()));
  54. int width = (int)(Double.parseDouble(tf3.getText()));
  55. int height = (int)(Double.parseDouble(tf1.getText()));
  56.  
  57. if (num <= 0 || delay <= 0 || width <= 0 || height <= 0)
  58. {
  59. errorLabel.setText("Please enter positive values only.");
  60. }
  61. else
  62. {
  63. errorLabel.setText("");
  64.  
  65. try
  66. {
  67. Robot rob = new Robot();
  68. for (int x = 0; x <= num; x++)
  69. {
  70. // what this is gonna do is move the mouse in random directions around the screen
  71. rob.mouseMove((int)(Math.random() * width), (int)(Math.random() * height));
  72. rob.delay(delay);
  73. }
  74. }
  75. catch(AWTException ex)
  76. {
  77. System.exit(0);
  78. }
  79. }
  80. }
  81. catch(Exception exx)
  82. {
  83. errorLabel.setText("Numbers only and no blank spaces!");
  84. }
  85. }
  86.  
  87. public static void main(String args[])
  88. {
  89. Win frame = new Win();
  90.  
  91. frame.setVisible(true);
  92. // frame.setSize(WIDTH, HEIGHT);
  93. frame.pack();
  94. frame.setTitle("Mouse Mover");
  95. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96. frame.setLocationRelativeTo(null);
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement