Java關於Robot類的使用以及遠端桌面的實現

來源:互聯網
上載者:User

標籤:des   label   實現   key   位置   延遲   取圖   event   return   

利用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:86462001210  *     @date 2017年4月16日 上午1:20:3211  *     <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 block30             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:86462001218  *     @date 2017年4月16日 下午4:43:1119  *     <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 block52                     e.printStackTrace();53                 }54             }55         } catch (AWTException e) {56             // TODO Auto-generated catch block57             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 }

以上兩個就是Robot類的使用demo,既然我已經用Robot類實現了自動操作,還有螢幕監控,那麼深入一下,如果再加入websocket實現即時通訊,那麼就可以實現遠程監控以及控制案頭的功能了。

Java關於Robot類的使用以及遠端桌面的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.