用java机器人实现抓屏!
时间:2004-10-17 来源:ilcj
import java.io.*; import java.awt.image.BufferedImage; import java.awt.*; import javax.swing.*; import java.awt.Robot; class DrawImg extends JPanel { Robot robot; BufferedImage bufImg; Image img; public DrawImg() { try { robot = new Robot(); } catch(Exception e) { e.printStackTrace(); } new Thread(new getCntImg(this)).start(); } class getCntImg implements Runnable { DrawImg di; public getCntImg(DrawImg di) { this.di = di; } public void run() { while(true) { try { bufImg = robot.createScreenCapture(new Rectangle( Toolkit.getDefaultToolkit().getScreenSize())); //将指定屏幕的象素COPY下来生成一个BufferedImage } catch(Exception e) { e.printStackTrace(); } int width = bufImg.getWidth(); int height = bufImg.getHeight(); img = bufImg.getScaledInstance(width, height, BufferedImage.TYPE_INT_RGB); try { Thread.sleep(1000); } catch(Exception e) { e.printStackTrace(); } } } } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, this); } } public class Robot_ extends JFrame { public Robot_() { super("screenCapture Demo"); setUndecorated(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setSize(new java.awt.Dimension(screenSize.width, screenSize.height)); this.getContentPane().add(new DrawImg()); } public static void main(String[] args) { Robot_ robot = new Robot_(); robot.show(); } }
相关阅读 更多 +