Java Robot應用樣本之機器人功能,javarobot

來源:互聯網
上載者:User

Java Robot應用樣本之機器人功能,javarobot

很多時候,我們希望能夠實現自動化的測試,自動示範功能,或者是其它的一些滑鼠和鍵盤控制的應用(比如幫人點擊廣告賺利潤等)。出於這樣的目的,自從JDK1.3開始,它就為我們提供了一個用來產生本機輸入事件的機器人類 — java.awt.Robot. 

  下面我來詳細介紹Robot的功能及應用樣本:

一、Robot主要的功能

1. BufferedImage createScreenCapture(Rectangle screenRect) 

說明:該方法提供類似於鍵盤上的PrintScreen鍵的功能,將指定矩形地區內的螢幕像素copy下來產生一個BufferedImage。
應用:我們可以將這個方法用在圖形程式中,或是用它來實現遠端螢幕傳輸,可做成遠端電腦監控程式等.

2. void delay(int ms)

說明:用來將當前的程式(thread)休眠(sleep)若干毫秒(ms)。
應用:可用來控製程序的延時。這個一般是必須的,因為你在兩次間隔操作中肯定有延時。

3. Color getPixelColor(int x, int y)

說明:取得給定螢幕座標像素位置的顏色值。
應用:就是取顏色RGB值,就不多說了。

4. void keyPress(int keycode)
 void keyRelease(int keycode)

說明:這兩個方法的作用一看便知,用來產生指定鍵的按鍵按下與抬起動作,相當於Win32 API的keyb_event函數,即類比鍵盤操作咯,具體keycode值就是KeyEvent.VK_C、KeyEvent.VK_D、KeyEvent.VK_CONTROL什麼的,具體應用時直接看Eclipse提示就知道了。
應用:可用於程式的自動示範、測試等,非常有用。

5. void mouseMove(int x, int y)

說明:將滑鼠游標移動到指定的螢幕座標。
應用:可用於程式的自動示範、測試等,配合其他的方法使用,是不可缺少的。

6. void mousePress(int buttons)
 void mouseRelease(int buttons)
 void mouseWheel(int wheelAmt)

說明:上面的三種方法,產生指定滑鼠按鍵的按下,抬起,及滾輪動作,就是類比滑鼠操作咯,具體buttons的值有InputEvent.BUTTON1_MASK(滑鼠左鍵)、InputEvent.BUTTON3_MASK(滑鼠右鍵,如果是雙鍵滑鼠,請改用InputEvent.BUTTON2_MASK)等。

應用:一樣也可用於程式的自動示範、測試等,配合其他方法使用,很重要。

二、應用執行個體

我寫了兩個比較小的應用執行個體,一個是簡單的類比測試,一個是自動點擊廣告賺利潤的,下面分別示範。

首先編寫一些公用的方法Common.java

package com.alexia;  import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage;  import javax.swing.Icon; import javax.swing.ImageIcon;  /**  * @description Robot協助類,實現基本的功能  * @author Alexia  * @date 2013/5/18  *  */ public class Common {    /**    * 按一下滑鼠(左擊),要雙擊就連續調用    *    * @param r    * @param x    *      x座標位置    * @param y    *      y座標位置    * @param delay    *      該操作後的延遲時間    */   public static void clickLMouse(Robot r, int x, int y, int delay) {     r.mouseMove(x, y);     r.mousePress(InputEvent.BUTTON1_MASK);     r.delay(10);     r.mouseRelease(InputEvent.BUTTON1_MASK);     r.delay(delay);    }    /**    * 滑鼠右擊,要雙擊就連續調用    *    * @param r    * @param x    *      x座標位置    * @param y    *      y座標位置    * @param delay    *      該操作後的延遲時間    */   public static void clickRMouse(Robot r, int x, int y, int delay) {     r.mouseMove(x, y);     r.mousePress(InputEvent.BUTTON3_MASK);     r.delay(10);     r.mouseRelease(InputEvent.BUTTON3_MASK);     r.delay(delay);    }    /**    * 鍵盤輸入(一次只能輸入一個字元)    *    * @param r    * @param ks    *      鍵盤輸入的字元數組    * @param delay    *      輸入一個鍵後的延遲時間    */   public static void pressKeys(Robot r, int[] ks, int delay) {     for (int i = 0; i < ks.length; i++) {       r.keyPress(ks[i]);       r.delay(10);       r.keyRelease(ks[i]);       r.delay(delay);     }   }    /**    * 複製    *    * @param r    * @throws InterruptedException    */   void doCopy(Robot r) throws InterruptedException {     Thread.sleep(3000);     r.setAutoDelay(200);     r.keyPress(KeyEvent.VK_CONTROL);     r.keyPress(KeyEvent.VK_C);     r.keyRelease(KeyEvent.VK_CONTROL);     r.keyRelease(KeyEvent.VK_C);   }    /**    * 粘貼    *    * @param r    * @throws InterruptedException    */   void doParse(Robot r) throws InterruptedException {     r.setAutoDelay(500);     Thread.sleep(2000);     r.mouseMove(300, 300);     r.mousePress(InputEvent.BUTTON1_MASK);     r.mouseRelease(InputEvent.BUTTON1_MASK);     r.keyPress(KeyEvent.VK_CONTROL);     r.keyPress(KeyEvent.VK_V);     r.keyRelease(KeyEvent.VK_CONTROL);     r.keyRelease(KeyEvent.VK_V);   }    /**    * 捕捉全屏慕    *    * @param r    * @return    */   public Icon captureFullScreen(Robot r) {     BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(         Toolkit.getDefaultToolkit().getScreenSize()));     ImageIcon icon = new ImageIcon(fullScreenImage);     return icon;   }    /**    * 捕捉螢幕的一個矯形地區    *    * @param r    * @param x    *      x座標位置    * @param y    *      y座標位置    * @param width    *      矩形的寬    * @param height    *      矩形的高    * @return    */   public Icon capturePartScreen(Robot r, int x, int y, int width, int height) {     r.mouseMove(x, y);     BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle(         width, height));     ImageIcon icon = new ImageIcon(fullScreenImage);     return icon;   }  } 

在樣本之前,注意螢幕座標位置如何確定,我是下載了一個小工具,用起來十分方便,建議大家使用

1. 簡單的類比測試

package com.alexia;  import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane;  public class SimpleTest {      public static void main(String[] args) throws Exception {      final Robot rb = new Robot();      new Thread() {       public void run() {         rb.delay(2000); // 類比斷行符號         rb.keyPress(KeyEvent.VK_ENTER);         rb.keyRelease(KeyEvent.VK_ENTER);       }     }.start();      rb.delay(3000);      // 設定開始菜單的大概位置     int x = 40;     int y = Toolkit.getDefaultToolkit().getScreenSize().height - 10; // 滑鼠移動到開始菜單,     rb.mouseMove(x, y);     rb.delay(500);      // 單擊開始菜單     Common.clickLMouse(rb, x, y, 500);          rb.delay(1000);      // 運行CMD命令cmd enter     int[] ks = { KeyEvent.VK_C, KeyEvent.VK_M,         KeyEvent.VK_D, KeyEvent.VK_ENTER, };     Common.pressKeys(rb, ks, 500);     rb.mouseMove(400, 400);     rb.delay(500);      // 運行DIR命令dir enter     ks = new int[] { KeyEvent.VK_D, KeyEvent.VK_I, KeyEvent.VK_R,         KeyEvent.VK_ENTER };     Common.pressKeys(rb, ks, 500);     rb.delay(1000);      // 運行CLS命令cls enter     ks = new int[] { KeyEvent.VK_C, KeyEvent.VK_L, KeyEvent.VK_S,         KeyEvent.VK_ENTER };     Common.pressKeys(rb, ks, 500);     rb.delay(1000);      // 運行EXIT命令exit enter     ks = new int[] { KeyEvent.VK_E, KeyEvent.VK_X, KeyEvent.VK_I,         KeyEvent.VK_T, KeyEvent.VK_ENTER };     Common.pressKeys(rb, ks, 500);     rb.delay(1000);      // 右鍵測試     x = Toolkit.getDefaultToolkit().getScreenSize().width - 50;     Common.clickRMouse(rb, x, y, 500);      new Thread() {       public void run() {         rb.delay(1000); // 斷行符號         rb.keyPress(KeyEvent.VK_ENTER);         rb.keyRelease(KeyEvent.VK_ENTER);       }     }.start();      JOptionPane.showMessageDialog(null, "示範完畢!");   } } 

2. 點擊網易廣告賺取微薄利潤

package com.alexia;  import java.awt.AWTException; import java.awt.Desktop; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; import java.net.URI; import java.util.Random;  public class AutoClickAds {    private Robot robot;    private volatile boolean stop = false;    /** Creates a new instance of Main */    public AutoClickAds() {      try {        robot = new Robot();      } catch (AWTException ex) {        ex.printStackTrace();      }   }    public void init() {      robot.delay(3000);          System.out.println("Click Ads start");      // 在新的瀏覽器視窗或在已有的瀏覽器視窗開啟指定的URL(JDK 1.6以上)     Desktop desktop = Desktop.getDesktop();     if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {       URI uri = URI.create("http://lanxuezaipiao.blog.163.com/");       try {         desktop.browse(uri);       } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();       }     }          try {       run();     } catch (InterruptedException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }      stop();      System.out.println("Click Ads stoped");    }    public void run() throws InterruptedException {     int count = 1;          while (!stop) {       robot.delay(8000);              int x = 576;       int y = 567;       Random r = new Random();        Common.clickLMouse(robot, x, y, 3000);        // 輸入向下箭頭,實現翻頁       int[] ks = { KeyEvent.VK_DOWN };       for (int i = 0; i < 10; i++)         Common.pressKeys(robot, ks, 0);        int[][] a = { { 500, 103 }, { 500, 163 }, { 500, 223 },           { 500, 283 }, { 500, 343 }, { 500, 403 }, { 500, 463 },           { 500, 523 }, { 500, 583 }, { 500, 643 }, };       int b = r.nextInt(5);       x = a[b][0];       y = a[b][1];        Common.clickLMouse(robot, x, y, 1000);        // 輸入向下箭頭,實現翻頁       for (int i = 0; i < 500; i++)         Common.pressKeys(robot, ks, 0);        // 輸入向下箭頭,實現翻頁       int[] kups = { KeyEvent.VK_UP };       for (int i = 0; i < 3; i++)         Common.pressKeys(robot, kups, 0);        x = 900;       y = 210;       Common.clickLMouse(robot, x, y, 3000);              x =1090;       y =15;       Common.clickLMouse(robot, x, y, 3000);              x = 900;       y = 135;       Common.clickLMouse(robot, x, y, 3000);        System.out.println("成功點擊第" + count + "個廣告!");     }    }    public synchronized void stop() {      stop = true;    }    /**    * * @param args the command line arguments    *    * @throws InterruptedException    */   public static void main(String[] args) throws InterruptedException {      AutoClickAds mc = new AutoClickAds();     mc.init();    } } 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.