Implement screen capture Remote Desktop control using Java

Source: Internet
Author: User
Tags gety
We mainly use the java. util. Robot class to capture the screen, which can capture a rectangular area of the screen. Through this class, we can also implement a remote desktop control program. Package COM. qiu. util; </P> <p> Import Java. io. *; </P> <p> Import java.net. *; </P> <p> Import javax. swing. *; </P> <p> Import Java. AWT. *; </P> <p> Import Java. AWT. event. *; </P> <p> Import Java. AWT. image. *; </P> <p> Import javax. imageIO. *; </P> <p> Import Java. io. *; </P> <p>/** @ author qiu_baichao </P> <p> * a simple screen capture </P> <p> * </P> <p>> **/</P> <p> public class screencapture {</P> <p> // test main </P> <p> Public static void main (string [] ARGs) throws exception {</P> <p> string userdir = system. getproperty ("user. dir "); </P> <p> file tempfile = new file (" D: "," temp.png "); </P> <p> screencapture capture = screencapture. getinstance (); </P> <p> capture. captureimage (); </P> <p> jframe frame = new jframe (); </P> <p> jpanel = new jpanel (); </P> <p> panel. setlayout (New borderlayout (); </P> <p> jlabel imagebox = new jlabel (); </P> <p> panel. add (borderlayout. center, imagebox); </P> <p> imagebox. seticon (capture. getpickedicon (); </P> <p> capture. savetofile (tempfile); </P> <p> capture. captureimage (); </P> <p> imagebox. seticon (capture. getpickedicon (); </P> <p> frame. setcontentpane (panel); </P> <p> frame. setsize (400,300); </P> <p> frame. show (); </P> <p> system. out. println ("over"); </P> <p >}</P> <p> private screencapture () {</P> <p> try {</P> <p> robot = new robot (); </P> <p >}</P> <p> catch (awtexception e) {</P> <p> system. err. println ("internal error:" + E); </P> <p> E. printstacktrace (); </P> <p >}</P> <p> jpanel CP = (jpanel) dialog. getcontentpane (); </P> <p> CP. setlayout (New borderlayout (); </P> <p> labfullscreenimage. addmouselistener (New mouseadapter () {</P> <p> Public void mousereleased (mouseevent EVn) {</P> <p> isfirstpoint = true; </P> <p> pickedimage = fullscreenimage. getsubimage (recx, recy, recw, rech); </P> <p> dialog. setvisible (false); </P> <p >}</P> <p >}); </P> <p> labfullscreenimage. addmousemotionlistener (New mousemotionadapter () {</P> <p> Public void mousedragged (mouseevent EVn) {</P> <p> If (isfirstpoint) {</P> <p> X1 = EVN. getx (); </P> <p> Y1 = EVN. gety (); </P> <p> isfirstpoint = false; </P> <p >}</P> <p> else {</P> <p> X2 = EVN. getx (); </P> <p> Y2 = EVN. gety (); </P> <p> int Maxx = math. max (x1, x2); </P> <p> int Maxy = math. max (Y1, Y2); </P> <p> int Minx = math. min (x1, x2); </P> <p> int miny = math. min (Y1, Y2); </P> <p> recx = Minx; </P> <p> recy = miny; </P> <p> recw = Maxx-Minx; </P> <p> rech = Maxy-miny; </P> <p> labfullscreenimage. drawrectangle (recx, recy, recw, rech ); </P> <p >}</P> <p> Public void mousemoved (mouseevent e) {</P> <p> labfullscreenimage. drawcross (E. getx (), E. gety (); </P> <p >}</P> <p >}); <br/> </P> <p> CP. add (borderlayout. center, labfullscreenimage); </P> <p> dialog. setcursor (cursor. getpredefinedcursor (cursor. crosshair_cursor); </P> <p> dialog. setalwaysontop (true); </P> <p> dialog. setmaximumsize (</P> <p> toolkit. getdefatooltoolkit (). getscreensize (); </P> <p> dialog. setundecorated (true); </P> <p> dialog. setsize (dialog. getmaximumsize (); </P> <p> dialog. setmodal (true); </P> <p >}</P> <p> // Singleton pattern </P> <p> Public static screencapture getinstance () {</P> <p> return defaultcapturer; </P> <p >}</P> <p>/** capture full screen MOOC */</P> <p> Public icon capturefullscreen () {</P> <p> fullscreenimage = robot. createscreencapture (New rectangle (</P> <p> toolkit. getdefatooltoolkit (). getscreensize (); </P> <p> imageicon = new imageicon (fullscreenimage); </P> <p> return icon; </P> <p >}</P> <p>/** capture an orthopedic area of the screen </P> <p> */</P> <p> public void captureimage () {</P> <p> fullscreenimage = robot. createscreencapture (New rectangle (</P> <p> toolkit. getdefatooltoolkit (). getscreensize (); </P> <p> imageicon = new imageicon (fullscreenimage); </P> <p> labfullscreenimage. seticon (icon); </P> <p> dicon. setvisible (true ); </P> <p >}</P> <p>/** obtain the captured bufferedimage */</P> <p> Public bufferedimage getpickedimage () {</P> <p> return pickedimage; </P> <p >}</P> <p>/** get the captured icon */</P> <p> Public imageicon getpickedicon () {</P> <p> return New imageicon (getpickedimage (); </P> <p >}</P> <p>/** stores the image as a file, PNG format </P> <p> * @ deprecated </P> <p> * Replaced by saveaspng (File file) </P> <p> **/</P> <p> @ deprecated </P> <p> Public void savetofile (File file) throws ioexception {</P> <p> ImageIO. write (getpickedimage (), defaultimageformater, file); </P> <p >}</P> <p>/*** save as a file, in PNG format */</P> <p> Public void saveaspng (File file) throws ioexception {</P> <p> ImageIO. write (getpickedimage (), "PNG", file ); </P> <p >}</P> <p>/** save as a JPEG image file */</P> <p> Public void saveasjpeg (File file File) throws ioexception {</P> <p> ImageIO. write (getpickedimage (), "Jpeg", file ); </P> <p >}</P> <p>/** write an outputstream */</P> <p> Public void write (outputstream out) throws ioexception {</P> <p> ImageIO. write (getpickedimage (), defaultimageformater, out ); </P> <p >}</P> <p> // Singleton Design Pattern </P> <p> Private Static screencapture defaultcapturer = new screencapture (); </P> <p> private int X1, Y1, X2, Y2; </P> <p> private int recx, recy, rech, recw; // captured image </P> <p> private Boolean isfirstpoint = true; </P> <p> private backgroundimage labfullscreenimage = new backgroundimage (); </P> <p> private Robot robot; </P> <p> private bufferedimage fullscreenimage; </P> <p> private bufferedimage pickedimage; </P> <p> private string defaultimageformater = "PNG"; </P> <p> private jdialog dialog = new jdialog (); </P> <p >}</P> <p>/** display image label */</P> <p> class backgroundimage extends jlabel {</P> <p> Public void paintcomponent (Graphics g) {</P> <p> super. paintcomponent (g); </P> <p> G. drawrect (X, Y, W, H); </P> <p> string area = integer. tostring (w) + "*" + integer. tostring (h); </P> <p> G. drawstring (area, x + (INT) W/2-15, Y + (INT) h/2); </P> <p> G. drawline (LinEx, 0, LinEx, getheight (); </P> <p> G. drawline (0, Liney, getwidth (), Liney); </P> <p >}</P> <p> Public void drawrectangle (int x, int y, int width, int height) {</P> <p> This. X = x; </P> <p> This. y = y; </P> <p> H = height; </P> <p> W = width; </P> <p> repaint (); </P> <p >}</P> <p> Public void drawcross (int x, int y) {</P> <p> LinEx = X; </P> <p> Liney = y; </P> <p> repaint (); </P> <p >}</P> <p> int LinEx, liney; </P> <p> int X, Y, H, W; </P> <p >}< br/>

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.