The Java Screenshot tool captures the screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.