Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.samkough.main;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- @SuppressWarnings("serial")
- public class Win extends JFrame implements ActionListener
- {
- // private static final int WIDTH = 300;
- // private static final int HEIGHT = 300;
- JLabel l1, l2, l3, l4, errorLabel;
- JTextField tf1, tf2, tf3, tf4;
- JButton b1;
- public Win()
- {
- // rows, columns, then the last two are for padding
- // horizontal padding and vertical padding
- setLayout(new GridLayout(5, 2, 5, 5));
- l1 = new JLabel("Enter number of movements:");
- l2 = new JLabel("Enter delay between movements milliseconds:");
- l3 = new JLabel("Enter screen width in pixels:");
- l4 = new JLabel("Enter screen height in pixels:");
- errorLabel = new JLabel("");
- tf1 = new JTextField();
- tf2 = new JTextField();
- tf3 = new JTextField();
- tf4 = new JTextField();
- b1 = new JButton("Start!");
- add(l1);
- add(tf1);
- add(l2);
- add(tf2);
- add(l3);
- add(tf3);
- add(l4);
- add(tf4);
- add(b1);
- add(errorLabel);
- b1.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- int num = (int)(Double.parseDouble(tf1.getText()));
- int delay = (int)(Double.parseDouble(tf2.getText()));
- int width = (int)(Double.parseDouble(tf3.getText()));
- int height = (int)(Double.parseDouble(tf1.getText()));
- if (num <= 0 || delay <= 0 || width <= 0 || height <= 0)
- {
- errorLabel.setText("Please enter positive values only.");
- }
- else
- {
- errorLabel.setText("");
- try
- {
- Robot rob = new Robot();
- for (int x = 0; x <= num; x++)
- {
- // what this is gonna do is move the mouse in random directions around the screen
- rob.mouseMove((int)(Math.random() * width), (int)(Math.random() * height));
- rob.delay(delay);
- }
- }
- catch(AWTException ex)
- {
- System.exit(0);
- }
- }
- }
- catch(Exception exx)
- {
- errorLabel.setText("Numbers only and no blank spaces!");
- }
- }
- public static void main(String args[])
- {
- Win frame = new Win();
- frame.setVisible(true);
- // frame.setSize(WIDTH, HEIGHT);
- frame.pack();
- frame.setTitle("Mouse Mover");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement