Java instance development 01-01 screen capture tool

Source: Internet
Author: User

Knowledge point:

1. Swing (developed using the Eclipse plug-in jigloo)

2. JPEG Encoder

 

Source code:

Package chapter01.sample01; import Java. AWT. dimension; import Java. AWT. image; import Java. AWT. rectangle; import Java. AWT. robot; import Java. AWT. toolkit; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. fileoutputstream; import javax. swing. imageicon; import javax. swing. jfilechooser; import javax. swing. jlabel; import javax. swing. jmenu; import javax. swing. jmenubar; import javax. swing. jmenuitem; import javax. swing. jscrollpane; import javax. swing. keystroke; import javax. swing. swingutilities; import javax. swing. windowconstants; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. required imageencoder; @ suppresswarnings ("restriction") public class capturescreen extends javax. swing. jframe {Private Static final long serialversionuid =-bytes; private jmenubar values; private jmenuitem jmenuitemclear; private jlabelshow; private jmenuitem values; private jmenu jmenu1; private jfilechooser filechooser = new jfilechooser (); // file selector private jsonimageencoder encoder = NULL; // JPEG encoder private image tempimage = NULL; /*** auto-generated main method to display this jframe */public static void main (string [] ARGs) {swingutilities. invokelater (New runnable () {public void run () {capturescreen inst = new capturescreen (); Inst. setlocationrelativeto (null); Inst. setvisible (true) ;}}) ;}public capturescreen () {super (); initgui () ;}private void initgui () {try {setdefaclocloseoperation (windowconstants. dispose_on_close); this. settitle ("captruescreentool"); getcontentpane (). setlayout (null); {jscrollpanegloba = new jscrollpane (); getcontentpane (). add (jscrollpanegloba); // jscrollpanegloba. setbounds (0, 0,384,234); // This is not the case. Otherwise, the jscrollpanegloba is not displayed when the form is expanded. setbounds (0, 0, toolkit. getdefatooltoolkit (). getscreensize (). width, toolkit. getdefatooltoolkit (). getscreensize (). height); {jlabelshow = new jlabel (); jscrollpanegloba. setviewportview (jlabelshow); // panel viewport property }}{ jmenubar1 = new jmenubar (); setjmenubar (jmenubar1); jmenubar1.setsize (384, 30); New Java. AWT. dimension (384, 30); {jmenu1 = new jmenu (); dimensions (jmenu1); jmenu1.settext ("\ u64cd \ u4f5c"); {jmenuitemcapture = new jmenuitem (); jmenu1.add (jmenuitemcapture); jmenuitemcapture. settext ("\ u622a \ u5c4f"); jmenuitemcapture. setaccelerator (keystroke. getkeystroke ("shift CTRL pressed P"); jmenuitemcapture. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent EVT) {region (EVT) ;}}{ jmenuitemsaveas = new jmenuitem (); jmenu1.add (jmenuitemsaveas); listener. settext ("\ u4fdd \ u5b58"); jmenuitemsaveas. setaccelerator (keystroke. getkeystroke ("shift pressed s"); jmenuitemsaveas. setenabled (false); jmenuitemsaveas. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent EVT) {saveimage () ;}}{ jmenuitemclear = new jmenuitem (); jmenu1.add (jmenuitemclear); jmenuclear. settext ("\ u6e05 \ u7a7a"); jmenuitemclear. setaccelerator (keystroke. getkeystroke ("shift CTRL pressed C"); jmenuitemclear. setenabled (false); jmenuitemclear. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent EVT) {invoke (EVT) ;}}{ jmenuitemexit = new jmenuitem (); jmenu1.add (jmenuitemexit); jmenuitemexit. settext ("\ u9000 \ u51fa"); jmenuitemexit. setaccelerator (keystroke. getkeystroke ("shift CTRL pressed X"); jmenuitemexit. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent EVT) {jmenuitemexitactionreceivmed (EVT) ;}}}} pack (); setsize (400,300 );} catch (exception e) {e. printstacktrace () ;}} private void jmenuitemcaptureactionreceivmed (actionevent EVT) {tempimage = This. createimage (); jlabelshow. seticon (New imageicon (tempimage); jmenuitemcapture. setenabled (false); jmenuitemsaveas. setenabled (true); jmenuitemclear. setenabled (true);} // screenshot: This is the focus. Save the image private image createimage () {try {image templocalimage = NULL; Robot robot = new robot (); dimension dimension = toolkit. getdefatooltoolkit (). getscreensize (); rectangle scrrectangle = new rectangle (0, 0, dimension. width, dimension. height); templocalimage = robot. createscreencapture (scrrectangle); Return templocalimage;} catch (exception e) {e. printstacktrace ();} return NULL;} // save: the code for saving the image is private void saveimage () {try {int save = filechooser. showsavedialog (this); // pops up a "save file" file chooser dialog. if (save = jfilechooser. approve_option) {// click OK (or yes) // obtain the file name to be set (including the path) file savefilename = filechooser. getselectedfile (); // get the name of the file to be saved string filename = new string (savefilename. getpath () + ". jpg "); // abstract pathname // 1. first create an output stream fileoutputstream = new fileoutputstream (filename); // 2. then the output stream is wrapped with a jpeg encoder. In fact, the output stream is connected to the encoder = jpegcodec. createjpegencoder (fileoutputstream); // encode the bufferedimage object with encoder. encode (bufferedimage) tempimage); fileoutputstream. flush (); // clear the cached fileoutputstream. close () ;}} catch (exception e) {e. printstacktrace () ;}}// clear Private void jmenuitemclearactionreceivmed (actionevent EVT) {jlabelshow. seticon (null); jmenuitemcapture. setenabled (true); jmenuitemclear. setenabled (false); jmenuitemsaveas. setenabled (false);} // exit private void jmenuitemexitactionreceivmed (actionevent EVT) {system. exit (0 );}}

Display Effect:

 

Captured image: The whole screen image at that time

Related Article

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.