6. Implement the screen using Java
Import Java. AWT. dimension; <br/> Import Java. AWT. rectangle; <br/> Import Java. AWT. robot; <br/> Import Java. AWT. toolkit; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. io. file; </P> <p> Import javax. imageIO. imageIO; </P> <p> /******************************** * ********************************* <br/> * use Java to implement the screen <br/> * this JavaBean can be called directly in other Java applications <SPAN class = 'wp _ keywordlink '> Program </span>, implement "photo" on the screen <br/> **************************************** * ************/</P> <p> public class guicamera {<br/> private string filename; // file prefix <br/> private string defaultname = "guicamera"; <br/> static int serialnum = 0; <br/> private string imageformat; // Image File Format <br/> private string defaultimageformat = "PNG"; <br/> dimension D = toolkit. getdefatooltoolkit (). getscreensize (); </P> <p> /******************************** * * ***************************** <Br/> * default file prefix is guicamera, file Format: PNG format the default construct will use the default <br/> * image file surname "guicamera ", and default image format "PNG" <br/> ***************************** *********************************/<br/> public guicamera () {<br/> filename = defaultname; <br/> imageformat = defaultimageformat; </P> <p >}</P> <p> /************************ ************ * ************************** <Br/> * @ Param S <br/> * the surname of the snapshot file <br/> * @ Param Format <br/> * The format of the image file, it can be "jpg" or "PNG" <br/> * This structure supports the storage of JPG and PNG files <br/> ************ **************************************** * ***********/<br/> Public guicamera (string s, string format) {</P> <p> filename = s; <br/> imageformat = format; <br/>}</P> <p> /******************** **************************************** * *** <Br/> * takes a screenshot of the screen, Snapshot the GUI once <br/> ******************* **************************************** * *****/<br/> Public void snapshot () {</P> <p> try {<br/> // copy the screen to a bufferedimage object screenshot <br/> bufferedimage screenshot = (new robot ()) <br/>. createscreencapture (New rectangle (0, 0, <br/> (INT) d. getwidth (), (INT) d. getheight (); <br/> serialnum ++; <br/> // Automatically generate a file name based on the file prefix variable and file format variable <br/> string name = filename + String. valueof (serialnum) + ". "<br/> + imageformat; <br/> file F = new file (name); <br/> system. out. print ("save file" + name); <br/> // write the screenshot object to the image file <br/> ImageIO. write (screenshot, imageformat, f); <br/> system. out. print (".. finished! /N "); <br/>}catch (exception ex) {<br/> system. out. println (Ex); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> guicamera cam = new guicamera ("D: // hello", "PNG"); // <br/> cam. snapshot (); <br/>}< br/>}