Java关于Robot类的使用

利用Robot类实现自动操作,实现效果是运行之后鼠标自动定位到整个屏幕坐标系的(635,454)位置,输入wangtianze

 1 package com.wtz.util;
 2 
 3 import java.awt.AWTException;
 4 import java.awt.Robot;
 5 import java.awt.event.InputEvent;
 6 import java.awt.event.KeyEvent;
 7 
 8 /**
 9  *     @author wangtianze QQ:864620012
10  *     @date 2017年4月16日 上午1:20:32
11  *     <p>description:Robot测试类,实现效果是运行之后鼠标自动定位到整个屏幕坐标系的(635,454)位置,输入wangtianze</p>
12  */
13 public class RobotTest {
14     public static void main(String[] args){
15         try {
16             Robot robot = new Robot();
17             //鼠标移动到坐标(635,454)
18             RobotTest.clickMouse(robot, 635, 454, 500);
19             
20             int[] keys = {
21                     KeyEvent.VK_W,KeyEvent.VK_A,
22                     KeyEvent.VK_N,KeyEvent.VK_G,
23                     KeyEvent.VK_T,KeyEvent.VK_I,
24                     KeyEvent.VK_A,KeyEvent.VK_N,
25                     KeyEvent.VK_Z,KeyEvent.VK_E};
26             robot.delay(500);
27             RobotTest.pressKey(robot,keys,500);
28         } catch (AWTException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32     }
33     
34     public static void pressKey(Robot robot,int[] keys,int delay){
35         for(int i=0;i<keys.length;i++){
36             robot.keyPress(keys[i]);
37             robot.keyRelease(keys[i]);
38             robot.delay(500);
39         }
40         //处理完需要延迟
41         robot.delay(delay);
42     }
43     
44     public static void clickMouse(Robot robot,int x,int y,int delay){
45         robot.mouseMove(x, y);
46         robot.delay(500);
47         robot.mousePress(InputEvent.BUTTON1_MASK);
48         robot.mouseRelease(InputEvent.BUTTON1_MASK);
49         robot.delay(delay);
50     }
51 }

利用Robot类实现监控屏幕,实现效果是运行后每隔50微秒获取当前屏幕图像,显示到JFrame内

 1 package com.wtz.util;
 2 
 3 import java.awt.AWTException;
 4 import java.awt.Dimension;
 5 import java.awt.Graphics2D;
 6 import java.awt.Rectangle;
 7 import java.awt.RenderingHints;
 8 import java.awt.Robot;
 9 import java.awt.Toolkit;
10 import java.awt.image.BufferedImage;
11 
12 import javax.swing.ImageIcon;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 
16 /**
17  *     @author wangtianze QQ:864620012
18  *     @date 2017年4月16日 下午4:43:11
19  *     <p>description:Robot实现监控屏幕,实现效果是每隔50微秒获取当前屏幕图像,显示到JFrame内</p>
20  */
21 public class RobotScreenTest {
22     public static void main(String[] args){
23         try {
24             Robot robot = new Robot();
25             JFrame jframe = new JFrame();
26             //设置标题
27             jframe.setTitle("wangtianze监控屏幕工具");
28             JLabel label = new JLabel();
29             jframe.add(label);
30             jframe.setSize(800,600);
31             //设置可见
32             jframe.setVisible(true);
33             //设置置顶
34             jframe.setAlwaysOnTop(true);
35             //控制台退出模式
36             jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37             //获取屏幕大小
38             Toolkit toolkit = Toolkit.getDefaultToolkit();
39             Dimension dm = toolkit.getScreenSize();
40             while(true){
41                 //一个矩形面板
42                 Rectangle rec = new Rectangle(0, 0, (int)dm.getWidth(), (int)dm.getHeight());
43                 //按照矩形截取图片到缓冲流
44                 BufferedImage img = robot.createScreenCapture(rec);
45                 //缩放图片
46                 BufferedImage newImg = RobotScreenTest.resize(img, jframe.getWidth(), jframe.getHeight());
47                 label.setIcon(new ImageIcon(newImg));
48                 try {
49                     Thread.sleep(50);
50                 } catch (InterruptedException e) {
51                     // TODO Auto-generated catch block
52                     e.printStackTrace();
53                 }
54             }
55         } catch (AWTException e) {
56             // TODO Auto-generated catch block
57             e.printStackTrace();
58         }
59     }
60     
61     public static BufferedImage resize(BufferedImage img, int newW, int newH){
62         int w = img.getWidth();
63         int h = img.getHeight();
64         //创建一个缩放后的图片流
65         BufferedImage newImg = new BufferedImage(newW,newH,img.getType());
66         Graphics2D g = newImg.createGraphics();
67         //设置模式
68         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
69         //按比例缩放
70         g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
71         g.dispose();
72         return newImg;
73     }
74 }

posted on 2017-04-16 17:01  wangtianze  阅读(18336)  评论(1编辑  收藏  举报

导航