Home > Java > javaTutorial > How to achieve random mouse movement effect in Java

How to achieve random mouse movement effect in Java

王林
Release: 2023-04-30 23:10:19
forward
1261 people have browsed it

Implementation code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * Java实现鼠标随机移动
 */
public class MouseController implements Runnable {

    private Robot robot;
    private boolean isStop = false;

    public MouseController() {
        try {
            ControllerFrame frame = new ControllerFrame("Prevent Locking");
            frame.setVisible(true);
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        int x;
        int y;
        Random random = new Random();
        while (!isStop) {
            //随机生成坐标。
            x = random.nextInt(1000);
            y = random.nextInt(1000);
            //开始移动
            robot.mouseMove(x, y);
            //每5秒移动一次
            robot.delay(6000);
        }

    }

    /**
     * GUI Frame 生成一个button,控制程序
     *
     * @author max
     */
    private class ControllerFrame extends JFrame {
        private static final long serialVersionUID = 1L;

        private JButton close = new JButton("close");

        public ControllerFrame(String title) {
            this();
            setTitle(title);
        }

        public ControllerFrame() {
            setLayout(new FlowLayout(FlowLayout.LEADING));
            setSize(100, 100);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            Dimension preferredSize = new Dimension(100, 60);
            Font font = new Font("", 1, 14);

            //设置button 大小,文字等属性
            close.setPreferredSize(preferredSize);
            close.setFont(font);
            close.setBorderPainted(true);
            close.setFocusable(false);

            add(close);

            //点击button后,程序终止。
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    isStop = true;
                    dispose();
                }
            });

        }

    }

    public static void main(String[] args) {
        MouseController m = new MouseController();
        m.run();
    }

}
Copy after login

Rendering

After running, a box will pop up, and then you just switch to the application window that will expire

How to achieve random mouse movement effect in Java

If you don’t want the mouse to continue moving, just click close

The above is the detailed content of How to achieve random mouse movement effect in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template