Java about the use of robot classes and the implementation of remote desktops

Source: Internet
Author: User

Using the robot class to achieve automatic operation, the effect is to run the mouse automatically positioned to the entire screen coordinate system (635,454) position, input Wangtianze

1  PackageCom.wtz.util;2 3 Importjava.awt.AWTException;4 ImportJava.awt.Robot;5 Importjava.awt.event.InputEvent;6 Importjava.awt.event.KeyEvent;7 8 /**9  *     @authorWangtianze qq:864620012Ten * @date April 16, 2017 morning 1:20:32 One * <p>description:robot Test class, the implementation of the effect is to run the mouse automatically positioned to the entire screen coordinate system (635,454) position, input wangtianze</p> A  */ -  Public classRobottest { -      Public Static voidMain (string[] args) { the         Try { -Robot Robot =NewRobot (); -             //Mouse moves to coordinates (635,454) -Robottest.clickmouse (robot, 635, 454, 500); +              -             int[] keys = { + Keyevent.vk_w,keyevent.vk_a, A Keyevent.vk_n,keyevent.vk_g, at Keyevent.vk_t,keyevent.vk_i, - Keyevent.vk_a,keyevent.vk_n, - keyevent.vk_z,keyevent.vk_e}; -Robot.delay (500); -Robottest.presskey (robot,keys,500); -}Catch(awtexception e) { in             //TODO auto-generated Catch block - e.printstacktrace (); to         } +     } -      the      Public Static voidPresskey (Robot Robot,int[] keys,intdelay) { *          for(inti=0;i<keys.length;i++){ $ robot.keypress (Keys[i]);Panax Notoginseng robot.keyrelease (Keys[i]); -Robot.delay (500); the         } +         //latency required after processing A Robot.delay (delay); the     } +      -      Public Static voidClickmouse (Robot Robot,intXintYintdelay) { $ robot.mousemove (x, y); $Robot.delay (500); - robot.mousepress (inputevent.button1_mask); - robot.mouserelease (inputevent.button1_mask); the Robot.delay (delay); -     }Wuyi}

Using the robot class to implement the monitoring screen, the effect is to run every 50 microseconds to get the current screen image, displayed in the JFrame

1  PackageCom.wtz.util;2 3 Importjava.awt.AWTException;4 Importjava.awt.Dimension;5 ImportJava.awt.Graphics2D;6 ImportJava.awt.Rectangle;7 Importjava.awt.RenderingHints;8 ImportJava.awt.Robot;9 ImportJava.awt.Toolkit;Ten ImportJava.awt.image.BufferedImage; One  A ImportJavax.swing.ImageIcon; - ImportJavax.swing.JFrame; - ImportJavax.swing.JLabel; the  - /** -  *     @authorWangtianze qq:864620012 - * @date April 16, 2017 PM 4:43:11 + * <p>description:robot Implementation monitor screen, achieve the effect is to get the current screen image every 50 microseconds, display to jframe inside </p> -  */ +  Public classRobotscreentest { A      Public Static voidMain (string[] args) { at         Try { -Robot Robot =NewRobot (); -JFrame JFrame =NewJFrame (); -             //Set Title -Jframe.settitle ("Wangtianze Monitor Screen Tool"); -JLabel label =NewJLabel (); in jframe.add (label); -Jframe.setsize (800,600); to             //Set Visible +Jframe.setvisible (true); -             //set up a sticky theJframe.setalwaysontop (true); *             //Console exit Mode $ jframe.setdefaultcloseoperation (jframe.exit_on_close);Panax Notoginseng             //Get screen size -Toolkit Toolkit =Toolkit.getdefaulttoolkit (); theDimension DM =toolkit.getscreensize (); +              while(true){ A                 //A rectangular panel theRectangle rec =NewRectangle (0, 0, (int) Dm.getwidth (), (int) Dm.getheight ()); +                 //capture a picture to a buffered stream by a rectangle -BufferedImage img =robot.createscreencapture (rec); $                 //Zoom Picture $BufferedImage newimg =robotscreentest.resize (IMG, Jframe.getwidth (), Jframe.getheight ()); -Label.seticon (NewImageIcon (newimg)); -                 Try { theThread.Sleep (50); -}Catch(interruptedexception e) {Wuyi                     //TODO auto-generated Catch block the e.printstacktrace (); -                 } Wu             } -}Catch(awtexception e) { About             //TODO auto-generated Catch block $ e.printstacktrace (); -         } -     } -      A      Public StaticBufferedImage Resize (bufferedimage img,intNEWW,intNEWH) { +         intW =img.getwidth (); the         inth =img.getheight (); -         //create a scaled picture stream $BufferedImage newimg =NewBufferedImage (Neww,newh,img.gettype ()); theGraphics2D g =newimg.creategraphics (); the         //Setup Mode the G.setrenderinghint (renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear); the         //Scale by scaling -G.drawimage (IMG, 0, 0, NEWW, NEWH, 0, 0, W, H,NULL); in g.dispose (); the         returnnewimg; the     } About}

The above two is the use of robot class demo, since I have used the robot class to achieve automatic operation, as well as screen monitoring, then in-depth, if you join WebSocket to achieve instant communication, then you can realize remote monitoring and control the function of the desktop.

Java about the use of robot classes and the implementation of remote desktops

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.