Java screenshot with mouse

Source: Internet
Author: User

The previous blog briefly describes the screenshot API of the robot class. However, there is no ready-made API to generate a screenshot with a mouse. The following is an example with a mouse screenshot.

The principle is to obtain the mouse position after screenshots. Use the java. AWT. graphics2d. drawimage method to draw the mouse. The Code is as follows:

Public class screensnapshot {public static void main (string [] ARGs) {try {Robot robot = new robot (); toolkit = toolkit. getdefatooltoolkit (); rectangle screen_area = new rectangle (Toolkit. getscreensize (); point P = mouseinfo. getpointerinfo (). getlocation (); bufferedimage screenshot = robot. createscreencapture (screen_area); string ImagePath = screensnapshot. class. getresource ("/"). getpath (). tostring (); bufferedimage cursor = ImageIO. read (new file (ImagePath + "cursor.gif"); // cursor.gif is your mouse image screenshot. creategraphics (). drawimage (cursor, P. x, p. y, null); ImageIO. write (screenshot, "jpg", new file ("C:/Cross/output.jpg"); ImageIO. write (screenshot, "PNG", new file ("C:/Cross/output.png");} catch (headlessexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (awtexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}

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.