Robot (java鼠标键盘-VAC)

# Robot ## guge ---url chrome://dino/小游戏 ``` Robot r=new Robot(); //创建自动化工具对象 r.delay(2000); //等2s // r.keyPress(KeyEvent.VK_3); //点击 // r.keyRelease(KeyEvent.VK_3); //放开 // RandomUtil.randomInt(100); //随机数100*/ // Color color = r.getPixelColor(28, 26); //获取坐标颜色 黑0 package com.wtl.tests.t2.dino; import java.awt.*; public class Dino { public static void main(String[] args) throws AWTException, InterruptedException { int x=470; int y=666; Color color1; Robot r=new Robot(); //创建自动化工具对象 color1 = r.getPixelColor(x, y); r.delay(3000); setXY(r,x, y); while (true){ color1 = r.getPixelColor(x, y); //获取坐标颜色 if (eqColor(color1.getRed(),83)){ r.keyPress(32); //点击 r.delay(100); r.keyRelease(32); //放开 } } } public static void setXY(Robot r, int x, int y){ for (int j = 0; j < 11; j++) { r.mouseMove(x, y); } } public static boolean eqColor(int r ,int rr ){ if (r==rr){ return true; } return false; } } ``` # ## [java获取屏幕坐标](https://blog.csdn.net/code_better/article/details/53505962) ``` package com.wtl.tests.t2; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import java.awt.Point; import java.util.Timer; import java.util.TimerTask; import java.awt.Color; public class MouseInfo extends JFrame { private final JPanel contentPanel = new JPanel(); JLabel value_x = null; JLabel value_y = null; /** * Launch the application. */ public static void main(String[] args) { try { MouseInfo info_frame = new MouseInfo(); info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); info_frame.setVisible(true); info_frame.setAlwaysOnTop(true); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Point point = java.awt.MouseInfo.getPointerInfo().getLocation(); // System.out.println("Location:x=" + point.x + ", y=" + // point.y); info_frame.value_x.setText("" + point.x); info_frame.value_y.setText("" + point.y); } }, 100, 100); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public MouseInfo() { setTitle("\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668"); setBounds(100, 100, 217, 156); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); JLabel lblx = new JLabel("\u5750\u6807x:"); lblx.setFont(new Font("宋体", Font.PLAIN, 15)); lblx.setBounds(22, 27, 66, 31); contentPanel.add(lblx); JLabel lbly = new JLabel("\u5750\u6807y:"); lbly.setFont(new Font("宋体", Font.PLAIN, 15)); lbly.setBounds(22, 68, 66, 31); contentPanel.add(lbly); value_x = new JLabel("0"); value_x.setForeground(Color.BLUE); value_x.setFont(new Font("宋体", Font.PLAIN, 20)); value_x.setBounds(82, 27, 66, 31); contentPanel.add(value_x); value_y = new JLabel("0"); value_y.setForeground(Color.BLUE); value_y.setFont(new Font("宋体", Font.PLAIN, 20)); value_y.setBounds(82, 68, 66, 31); contentPanel.add(value_y); } } ``` ## [java获取桌面截图](https://blog.csdn.net/kevon_sun/article/details/80164510?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-7.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-7.channel_param) ``` package com.wtl.tests.t2; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class CaptureScreen { public static void captureScreen(String fileName, String folder) throws Exception { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screenRectangle = new Rectangle(screenSize); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(screenRectangle); //保存路径 File screenFile = new File(fileName); if (!screenFile.exists()) { screenFile.mkdir(); } File f = new File(screenFile, folder); ImageIO.write(image, "png", f); //自动打开 if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) Desktop.getDesktop().open(f); } public static void main(String[] args) { try { captureScreen("D:\\" ,"1.png"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ```