Write a screenshot tool in Java

Source: Internet
Author: User
Tags gety

Using Java to imitate QQ

First define a startup class, run this method

1 ImportJava.awt.Rectangle;2 ImportJava.awt.Robot;3 ImportJava.awt.Toolkit;4     5      Public classScreen {6         7          Public Static voidMain (string[] args)throwsException {8             intWidth= (int) Toolkit.getdefaulttoolkit (). Getscreensize (). getwidth ();9             intHeight= (int) Toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ();TenRobot r=NewRobot (); One             NewView (R.createscreencapture (NewRectangle (0, 0, width, height )); A         } -     } -     

Initializing screenshots

Import Java.awt.toolkit;import java.awt.image.bufferedimage;import Javax.swing.imageicon;import javax.swing.JFrame ; Import Javax.swing.jpanel;public class View extends JFrame {bufferedimage image; BufferedImage Srcimage; JPanel Jp;imageicon icon; View (bufferedimage image) {int width = (int) toolkit.getdefaulttoolkit (). Getscreensize (). getwidth (); int height = (int) Toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight (); this.image = Image;icon = new ImageIcon (image); jp = new Huabu ( image); This.add (JP); this.setsize (width, height); this.setundecorated (true); This.setdefaultcloseoperation ( Jframe.exit_on_close); this.setvisible (True);}}

Mouse Listener Events

1 ImportJava.awt.Color;2 ImportJava.awt.Graphics;3 Importjava.awt.event.ActionEvent;4 ImportJava.awt.event.ActionListener;5 Importjava.awt.event.MouseEvent;6 ImportJava.awt.event.MouseListener;7 ImportJava.awt.event.MouseMotionListener;8 ImportJava.awt.image.BufferedImage;9 ImportJava.io.File;Ten ImportJava.io.FileOutputStream; One Importjava.util.Date; A  - ImportJavax.imageio.ImageIO; - ImportJavax.swing.JButton; the ImportJavax.swing.JOptionPane; - ImportJavax.swing.JPanel; -  -  Public classHuabuextendsJPanelImplementsMouseListener, + Mousemotionlistener, ActionListener { - bufferedimage image; +     intSX = 0; A     intSY = 0; at     inttx = 0; -     intty = 0; -     BooleanFlag =true; -JButton save =NewJButton ("Save"); -  - Huabu (bufferedimage image) { in          This. setlayout (NULL); -          This. Image =image; to          This. Addmouselistener ( This); +          This. Addmousemotionlistener ( This); -Save.addactionlistener ( This); the     } *  $ @OverridePanax Notoginseng      Public voidPaint (Graphics g) { -         //TODO auto-generated Method Stub theG.drawimage (image, 0, 0,NULL); + Draw (g); A  the     } +  -     Private voidDraw (Graphics g) { $ G.setcolor (Color.cyan); $ g.drawline (SX, SY, TX, SY); - g.drawline (SX, Ty, TX, ty); - g.drawline (SX, SY, SX, Ty); the G.drawline (TX, Sy, TX, ty); -System.out.println ("Draw" + tx + "" +ty);Wuyi     } the  - @Override Wu      Public voidmousedragged (MouseEvent e) { -  Abouttx =e.getx (); $Ty =e.gety (); -          This. Repaint (); -SYSTEM.OUT.PRINTLN ("Change target" + tx + "" +ty); -  A     } +  the @Override -      Public voidmousemoved (MouseEvent e) { $         //TODO auto-generated Method Stub the  the     } the  the @Override -      Public voidmouseclicked (MouseEvent e) { in  the     } the  About @Override the      Public voidmousepressed (MouseEvent e) { the         if(flag) { theSYSTEM.OUT.PRINTLN ("Change the original coordinates"); +              This. Addmousemotionlistener ( This); -SX =e.getx (); theSY =e.gety ();Bayi         } the     } the  - @Override -      Public voidmousereleased (MouseEvent e) { theFlag =false; the         intx = TX > SX?tx:sx; the         inty = ty > sy?Ty:sy; theSave.setbounds (x-100, y + 3, 100, 30); -          This. Add (save); theSave.setvisible (true); the Save.requestfocus (); the          This. Repaint ();94  the     } the  the @Override98      Public voidmouseentered (MouseEvent e) { About         //TODO auto-generated Method Stub - 101     }102 103 @Override104      Public voidmouseexited (MouseEvent e) { the         //TODO auto-generated Method Stub106 107     }108 109 @Override the      Public voidactionperformed (ActionEvent e) {111         if(E.getsource () = =Save) { theString name =NewDate (). GetTime () + "";113File f =NewFile ("d:\\" + name + ". jpg"); the FileOutputStream out; the             Try { theout =NewFileOutputStream (f);117System.out.println ("save" + tx + "" +ty);118 119                 if(SX >tx) { - Exchange (SX, TX);121                 }122                 if(Sy >ty) {123 Exchange (sy, Ty);124                 } theImageio.write (Image.getsubimage (SX, SY, TX-SX, Ty-sy),126"JPG", out);127 out.close (); -Joptionpane.showmessagedialog (NULL, "picture" + Name + "successfully saved to D: Disk");129System.exit (1); the}Catch(Exception E1) {131 e1.printstacktrace (); the             }133         }134     }135 136     Private voidExchangeintSy2,intty2) {137         inttemp =Sy2;138Sy2 =ty2;139Ty2 =temp; $     }141 142}

Write a tool in Java

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.