Java Tutorial/Development/Robot — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:30, 31 мая 2010
Содержание
Capture a screenshot
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class Main {
public static void main(String[] argv) throws Exception {
Robot robot = new Robot();
int x = 100;
int y = 100;
int width = 200;
int height = 200;
Rectangle area = new Rectangle(x, y, width, height);
BufferedImage bufferedImage = robot.createScreenCapture(area);
// Capture the whole screen
area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
bufferedImage = robot.createScreenCapture(area);
}
}
Capturing a Screen Shot
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
public class Main {
public static void main(String[] argv) throws Exception {
Robot robot = new Robot();
int x = 100;
int y = 100;
int width = 200;
int height = 200;
Rectangle area = new Rectangle(x, y, width, height);
BufferedImage bufferedImage = robot.createScreenCapture(area);
area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
bufferedImage = robot.createScreenCapture(area);
}
}
Capturing Screen in an image using Robot class
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
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;
import javax.swing.JWindow;
public class Main {
public static void main(String[] arg) throws Exception {
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, (int) screenDim.getWidth(),
(int) screenDim.getHeight()));
JWindow window = new JWindow(new JFrame());
window.getContentPane().setLayout(new BorderLayout());
window.getContentPane().add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
window.pack();
window.setVisible(true);
}
}
Create key press event using Robot class?
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.delay(3000);
robot.keyPress(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_Y);
}
}
Create mouse event using Robot class
import java.awt.Robot;
import java.awt.event.InputEvent;
public class Main {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.mouseMove(200, 200);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mouseWheel(-100);
}
}
Get the colour of a screen pixel
import java.awt.Color;
import java.awt.Robot;
public class Main {
public static void main(String[] args) throws Exception{
Robot robot = new Robot();
Color color = robot.getPixelColor(20, 20);
System.out.println("Red = " + color.getRed());
System.out.println("Green = " + color.getGreen());
System.out.println("Blue = " + color.getBlue());
}
}
Moving the Cursor on the Screen
import java.awt.Robot;
public class Main {
public static void main(String[] argv) throws Exception {
Robot robot = new Robot();
robot.mouseMove(500, 500);
}
}
Simulate a key press
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] argv) throws Exception {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
}
}
Simulate a mouse click
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class Main {
public static void main(String[] argv) throws Exception {
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}