Java by API/java.awt/Robot

Материал из Java эксперт
Перейти к: навигация, поиск

Robot: createScreenCapture(Rectangle screenRect)

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Robot: getPixelColor(int x, int y)

   <source lang="java">

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());
 }

}

 </source>
   
  
 
  



Robot: keyPress(int keycode)

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Robot: keyRelease(int keycode)

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Robot: mouseMove(int x, int y)

   <source lang="java">

import java.awt.Robot; import java.awt.event.InputEvent; public class MainClass {

 public static void main(String[] args) throws Exception{
   Robot r = new Robot();
   r.mouseMove(35,35);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
   Thread.sleep(50);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
 }

}


 </source>
   
  
 
  



Robot: mousePress(int buttons)

   <source lang="java">

import java.awt.Robot; import java.awt.event.InputEvent; public class MainClass {

 public static void main(String[] args) throws Exception{
   Robot r = new Robot();
   r.mouseMove(35,35);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
   Thread.sleep(50);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
 }

}


 </source>
   
  
 
  



Robot: mouseRelease(int buttons)

   <source lang="java">

import java.awt.Robot; import java.awt.event.InputEvent; public class MainClass {

 public static void main(String[] args) throws Exception{
   Robot r = new Robot();
   r.mouseMove(35,35);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
   Thread.sleep(50);
   r.mousePress( InputEvent.BUTTON1_MASK );
   r.mouseRelease( InputEvent.BUTTON1_MASK );
 }

}


 </source>
   
  
 
  



Robot: mouseWheel(int wheelAmt)

   <source lang="java">

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);
 }

}


 </source>