java控制鼠标自动点击

评价:
0
(0用户)
package util;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class 鼠标点击 extends JFrame {
    public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss");

    public static Robot robot;
    public static Dimension screen;

    public static void init() throws AWTException {
        screen = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
        //System.out.println(screen.width);
        //System.out.println(screen.height);

        //Point location = MouseInfo.getPointerInfo().getLocation(); // 获取鼠标当前位置
        //System.out.println(location.getX());
        //System.out.println(location.getY());

        robot = new Robot();
        //在处理完当前事件队列只的所有事件之前,一直等待
        //robot.waitForIdle();
        //设置此robot在生成一个事件后是否自动调用waitForIdle(),设置为true,表示添加的事件逐个按顺序执行
        robot.setAutoWaitForIdle(Boolean.TRUE);
        //设置此robot每在生成一个事件后自动睡眠的毫秒数
        //robot.setAutoDelay(2000);
        //获取指定矩形区域的图像(截图)
        //BufferedImage bufferedimage=robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        //ImageIcon icon = new ImageIcon(fullScreenImage);

        robot.delay(3000);
    }

    public static void start() {
        new Thread() {
            public void run() {
                //编辑
                mouseClick(1157, 354);
                robot.delay(2000);

                //编辑连接
                for (int i = 0; i < 3; i++) {
                    mouseClick2(1340, 726);
                }
                robot.delay(500);
                Color beginColor1 = new Color(199, 0, 11);
                Color beginColor2 = robot.getPixelColor(180, 687);
                if (beginColor1.equals(beginColor2)) {
                    robot.delay(1000);
                    mouseClick(214, 687);
                    robot.delay(5000);
                }

                //滚动
                //				robot.mouseMove(1341, 134);
                //				Color rollColor1 = new Color(238, 240, 245);
                //				Color rollColor2 = robot.getPixelColor(1341, 134);
                //				if (rollColor2.equals(rollColor1)) {
                //					//robot.mouseWheel(100);
                //				}

                //编辑字段
                for (int i = 0; i < 200; i++) {
                    mouseClick2(1340, 726);
                }
                robot.delay(500);
                Color beginColor3 = new Color(199, 0, 11);
                Color beginColor4 = robot.getPixelColor(270, 687);
                if (beginColor3.equals(beginColor4)) {
                    robot.delay(3000);
                    mouseClick(307, 687);
                    robot.delay(2000);
                } else {
                    for (int i = 0; i < 200; i++) {
                        mouseClick2(1340, 726);
                    }
                    if (beginColor3.equals(beginColor4)) {
                        robot.delay(3000);
                        mouseClick(307, 687);
                        robot.delay(2000);
                    } else {
                        robot.delay(3000);
                        mouseClick(307, 687);
                        robot.delay(2000);
                    }
                }

                //保存并运行
                Color beginColor5 = new Color(199, 0, 11);
                Color beginColor6 = robot.getPixelColor(350, 530);
                if (beginColor5.equals(beginColor6)) {
                    mouseClick(395, 530);
                    robot.delay(2000);
                }

                //鼠标归位
                robot.mouseMove(screen.width / 2, screen.height / 2);
            }
        }.start();
    }

    //移动并点击鼠标
    public static void mouseClick(int x, int y) {
        robot.mouseMove(x, y);
        robot.delay(300);
        //鼠标左键:BUTTON1_DOWN_MASK	鼠标中键:BUTTON2_DOWN_MASK	鼠标右键:BUTTON3_DOWN_MASK
        robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
    }

    //移动并点击鼠标
    public static void mouseClick2(int x, int y) {
        robot.mouseMove(x, y);
        //鼠标左键:BUTTON1_DOWN_MASK	鼠标中键:BUTTON2_DOWN_MASK	鼠标右键:BUTTON3_DOWN_MASK
        robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
    }

    public static void main(String[] args) throws Exception {
        //初始化
        init();

        int count = 1;
        while (true) {
            System.out.print("第 " + (count++) + " 次");

            //校准特殊颜色是否正确	不在界面不点击
            Color beginColor1 = new Color(199, 0, 11);
            Color beginColor2 = robot.getPixelColor(90, 227);
            Color beginColor3 = robot.getPixelColor(90, 257);
            if (beginColor1.equals(beginColor2) || beginColor1.equals(beginColor3)) {
                System.out.println("\t开始点击!");
                //开始点击
                start();
            } else {
                System.out.println("\t跳过!");
            }

            Thread.sleep(5 * 1000);
        }
    }
}



 

本文为原创文章,转载请注明出处!

注册并通过认证的用户才可以进行评价!

admin:支持一下,感谢分享!,+10,  

发表评论