站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题java中“机器人

java中“机器人

摘要: java中“机器人

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
<table width="681" border="0"> <tr> <td width="404"> 在Java的 JDK中就有个有趣的类,名为Robot(机器人)。这个类主要用来产生使用者本身这个系统事件,或根据使用者的需要(例如程序需要自动展示一些功能等)来控制鼠标或键盘。

在 Robot 这个类中产生的事件有别于在AWT元件中的事件,在AWT 元件中的事件(例如 MouseEvent、KeyEvent等)会被放置在 AWT的事件队列中等候处理。但您用 Robot 这个类别所产生的事件,则会被放置在操作系统的事件队列中等候处理。例如,当你用了 Robot 中的 Robot.mouseMove() 这个方法,就表示真的让您的鼠标移动了,而不像 AWT 事件中只是产生鼠标移动的事件而已。

在这里要注意的是,由于 Robot 产生了一些较低阶的控制,所以一旦当前系统不允许您从事这些控制时,就会发出 AWTException 这个例程来通知您。 </td> <td width="267" align="right"> </td> </tr> </table>
import java.awt.*;
import java.awt.event.*;
public class example1 {
Robot robot=null;
void initial() {
try {
// 产生一个 Robot 类别的物件
robot = new Robot();
}catch (AWTException ex) {}
}

void start() {
// 一开始希望鼠标移动到的座标
int initX = 30, initY = 10;
//然后希望移动到的鼠标座标
int disX = 120, disY = 20;
// 目前鼠标的位置座标
int mouseX, mouseY;
mouseX = initX;
mouseY = initY;
// 延迟了 3 秒钟
robot.delay(3000);
// 移动到 左上角第一个图标的位置
robot.mouseMove(30,10);
// 鼠标按下了鼠标右键
robot.mousePress(InputEvent.BUTTON3_MASK);
// 鼠标放开所按下的右键
robot.mouseRelease(InputEvent.BUTTON3_MASK);
// 停留 1 秒钟
robot.delay(1000);
// 鼠标移动到“打开”的位置
for(int i = 0 ; mouseX < disX ; i++) {
mouseX = mouseX + i;
robot.mouseMove(mouseX, mouseY);
robot.delay(50);
}

for(int j = 0 ; mouseY < disY ; j++) {
mouseY = mouseY + j;
robot.mouseMove(mouseX, mouseY);
robot.delay(50);
}
// 停留 1 秒钟后按下左键
robot.delay(1000);
robot.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
public static void main(String[] args) {
example1 ex=new example1();
ex.initial();
ex.start();
System.exit(0);
}
}

在Robot类中有一个比较特殊的method:
BufferedImage createScreenCapture(Rectangle screenRect)
顾名思义,当我们使用这个这个method 时,就会建立一张影像,而这张影像中存的的则是我们从屏幕撷取出来的像素(pixel)。而使用这个method,就会传回一个BufferedImage类的对象,我们可以根据我们的需要来对它进行处理。下面是个简单的范例,在这个范例中,程序会在使用者希望停留的秒数钟后截取屏幕,并将文件用jpg格式保存。
/*
* @(#)ScreenCapture.java 2001/09/24
* Usage: java ScreenCapture [-d seconds] [-f filename]
* @Author
* Cheng-Yu Chang
* @License
* GPL(GNU GENERAL PUBLIC LICENSE)
* http://www.gnu.org/copyleft/gpl.html
*/
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ScreenCapture {
public static void ScreenCapture(int seconds, String filename) {
Robot robot = null;
// 产生 Robot 物件,记着要 catch AWTException
try {
robot =new Robot();
// 延迟使用者输入的秒数
robot.delay(seconds);
// 将截取的image转换成jpg格式
OutputStream f = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);
encoder.encode(robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())));
f.close();
}catch(AWTException e1) {}
catch(IOException e2) {}
}

// 使用说明

private static void usage() {
System.out.println("Usage: java ScreenCapture [-d seconds] [-f filename]");
System.out.println("\t-d Seconds to delay before capturing screen");
System.out.println("\t-f JPG filename to save");
System.exit(0);
}

public static void main(String args[]) {
int s = 0;
String filename = "ScreenCapture.jpg";
// 判定输入的参数
if (args.length == 0)
usage();
else {
for (int i = 0 ; i < args.length ; i++) {
if (args[i].startsWith("-")) {
if (args[i].equals("-d")) {
if (i < args.length - 1)
s = Integer.parseInt(args[++i]);
else
usage();
}

if (args[i].equals("-f")) {
if (i < args.length - 1)
filename = args[++i];
else
usage();
}
}else
usage();
}
}
ScreenCapture(s * 1000, filename);
System.exit(0);
}

}
</td> </tr> <tr>


↑返回目录
前一篇: 老鼠走迷宫
后一篇: 做一个颜色渐变的Panel