国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java Robot應用示例之機器人功能

2024-07-13 10:17:32
字體:
來源:轉載
供稿:網友

很多時候,我們希望能夠實現自動測試,自動演示功能,或者是其它的一些鼠標和鍵盤控制的應用(比如幫人點擊廣告賺利潤等)。出于這樣的目的,自從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();    } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中江县| 雅安市| 福贡县| 神农架林区| 阿坝县| 昌乐县| 钦州市| 河池市| 南雄市| 苏尼特左旗| 漳浦县| 乌苏市| 三台县| 甘德县| 玛多县| 分宜县| 恩施市| 当雄县| 略阳县| 鄂温| 府谷县| 汕尾市| 通化市| 灵丘县| 乌兰县| 瓦房店市| 炉霍县| 台安县| 普安县| 凤阳县| 景泰县| 昆明市| 锦屏县| 岑溪市| 宁国市| 德兴市| 金山区| 方正县| 武安市| 微博| 博罗县|