Java 怎么创建机器人

2019年8月21日22:01:13 评论 23

1.简介

Java 怎么创建机器人?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.awt.AWTException;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import java.awt.MouseInfo;

import java.awt.Robot;

import java.util.concurrent.atomic.AtomicReference;

public class Main {
    public static Robot createRobot(GraphicsDevice device) {
        try {
            return new Robot(device);
        } catch (AWTException e) {
            throw new RuntimeException("createRobot", e);
        }
    }

    public static Robot createRobot(
            AtomicReference<GraphicsDevice> deviceRef) {
        GraphicsDevice device = getMouseScreen();
        deviceRef.set(device);
        return createRobot(device);
    }

    public static GraphicsDevice getMouseScreen() {
        return MouseInfo.getPointerInfo().getDevice();
    }

    public static GraphicsDevice getDevice(int screenNo)
            throws AWTException {
        GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getScreenDevices()[screenNo];
        return device;
    }
}

 

3.总结

示例代码演示了 Java 怎么创建机器人,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: