Java关于Robot类的使用以及远程桌面的实现

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

 package com.wtz.util;

 import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent; /**
* @author wangtianze QQ:864620012
* @date 2017年4月16日 上午1:20:32
* <p>description:Robot测试类,实现效果是运行之后鼠标自动定位到整个屏幕坐标系的(635,454)位置,输入wangtianze</p>
*/
public class RobotTest {
public static void main(String[] args){
try {
Robot robot = new Robot();
//鼠标移动到坐标(635,454)
RobotTest.clickMouse(robot, 635, 454, 500); int[] keys = {
KeyEvent.VK_W,KeyEvent.VK_A,
KeyEvent.VK_N,KeyEvent.VK_G,
KeyEvent.VK_T,KeyEvent.VK_I,
KeyEvent.VK_A,KeyEvent.VK_N,
KeyEvent.VK_Z,KeyEvent.VK_E};
robot.delay(500);
RobotTest.pressKey(robot,keys,500);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void pressKey(Robot robot,int[] keys,int delay){
for(int i=0;i<keys.length;i++){
robot.keyPress(keys[i]);
robot.keyRelease(keys[i]);
robot.delay(500);
}
//处理完需要延迟
robot.delay(delay);
} public static void clickMouse(Robot robot,int x,int y,int delay){
robot.mouseMove(x, y);
robot.delay(500);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(delay);
}
}

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

 package com.wtz.util;

 import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage; import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel; /**
* @author wangtianze QQ:864620012
* @date 2017年4月16日 下午4:43:11
* <p>description:Robot实现监控屏幕,实现效果是每隔50微秒获取当前屏幕图像,显示到JFrame内</p>
*/
public class RobotScreenTest {
public static void main(String[] args){
try {
Robot robot = new Robot();
JFrame jframe = new JFrame();
//设置标题
jframe.setTitle("wangtianze监控屏幕工具");
JLabel label = new JLabel();
jframe.add(label);
jframe.setSize(800,600);
//设置可见
jframe.setVisible(true);
//设置置顶
jframe.setAlwaysOnTop(true);
//控制台退出模式
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//获取屏幕大小
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dm = toolkit.getScreenSize();
while(true){
//一个矩形面板
Rectangle rec = new Rectangle(0, 0, (int)dm.getWidth(), (int)dm.getHeight());
//按照矩形截取图片到缓冲流
BufferedImage img = robot.createScreenCapture(rec);
//缩放图片
BufferedImage newImg = RobotScreenTest.resize(img, jframe.getWidth(), jframe.getHeight());
label.setIcon(new ImageIcon(newImg));
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static BufferedImage resize(BufferedImage img, int newW, int newH){
int w = img.getWidth();
int h = img.getHeight();
//创建一个缩放后的图片流
BufferedImage newImg = new BufferedImage(newW,newH,img.getType());
Graphics2D g = newImg.createGraphics();
//设置模式
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//按比例缩放
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return newImg;
}
}
上一篇:Spring MVC 返回NULL时客户端用$.getJSON的问题


下一篇:Caused by: java.sql.SQLException: Field 'category_id' doesn't have a default value