Original: http://www.open-open.com/code/view/1420037709781
Importjava.awt.BorderLayout;ImportJava.awt.Container;Importjava.awt.Dimension;ImportJava.awt.Graphics;ImportJava.awt.Rectangle;ImportJava.awt.Robot;ImportJava.awt.Toolkit;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.image.BufferedImage;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;Importjavax.swing.WindowConstants;/*** Capture screen, screen tool*/ Public classScreencaptureframeextendsJFrameImplementsActionListener {PrivateScreencaptureutil Scrcaptureutil =NULL;//tool classes for capturing screens PrivatePaintcanvas Canvas =NULL;//canvas, used to draw captured screen images PublicScreencaptureframe () {Super("Screen Capture"); Init (); } /*** Initialize*/ Private voidinit () {Scrcaptureutil=NewScreencaptureutil ();//Create a screen capture toolCanvas =NewPaintcanvas (Scrcaptureutil);//Create a canvasContainer C= This. Getcontentpane (); C.setlayout (NewBorderLayout ()); C.add (canvas, borderlayout.center); JButton Capbutton=NewJButton ("Grab screen"); C.add (Capbutton, Borderlayout.south); Capbutton.addactionlistener ( This); This. setSize (400, 400); This. setvisible (true); This. Setdefaultcloseoperation (Windowconstants.exit_on_close); } Public voidactionperformed (ActionEvent e) {//draw a screen image on the canvas when you click the "Grab Screen" buttonCanvas.drawscreen (); } Public Static voidMain (string[] args) {NewScreencaptureframe (); }}/*** Grab screen tool class*/classScreencaptureutil {PrivateRobot Robot =NULL;//the main tool class for screen capture PrivateRectangle Scrrect =NULL;//rectangular image of the screen PublicScreencaptureutil () {Try{robot=NewRobot ();//Create a screen capture tool}Catch(Exception ex) {System.out.println (ex.tostring ()); } //get a rectangular image of the screenDimension scrsize =Toolkit.getdefaulttoolkit (). Getscreensize (); Scrrect=NewRectangle (0, 0, Scrsize.width, scrsize.height); } /*** Screen Capture method * *@returnreturns an image*/ Publicbufferedimage Capturescreen () {bufferedimage scrimg=NULL; Try{scrimg= Robot.createscreencapture (Scrrect);//Grab a full screen image.}Catch(Exception e) {System.out.println (e.tostring ()); } returnscrimg; }}/*** Canvas class, used to display the image captured by the screen*/classPaintcanvasextendsJPanel {PrivateScreencaptureutil Scrcaptureutil =NULL;//Screen Capture Tool PrivateBufferedImage scrimg =NULL;//images to be drawn PublicPaintcanvas (Screencaptureutil screenutil) { This. Scrcaptureutil =Screenutil; } /*** Heavy-duty JPanel paintcomponent for background painting*/ protected voidpaintcomponent (Graphics g) {if(Scrimg! =NULL) { intIwidth = This. getwidth (); intIheight = This. GetHeight (); G.drawimage (scrimg,0, 0, iwidth, iheight, 0, 0, Scrimg.getwidth (), Scrimg.getheight (),NULL); } } /*** How to draw a screen image*/ Public voidDrawscreen () {Graphics g= This. getgraphics (); Scrimg= Scrcaptureutil.capturescreen ();//Capture the screen image if(Scrimg! =NULL) { This. paintcomponent (g);//Drawing} g.dispose ();//Freeing Resources }}
Java Screen Tool capture screen