The example of this article describes the implementation method of Java simulation QQ desktop screenshots. Share it for everyone for your reference. The specifics are as follows:
The desktop screenshot function of QQ is very convenient. Last year, I used Java to simulate one, and it is now sorted out.
This method first needs to catch the entire image of the screen, display the image in a JFRAME, and then display the full screen of JFRAME. In this way, a desktop is simulated, and Java can get the action area of the mouse to achieve the small on the desktop in the desktop Screen screenshot.
Import javax.swing.*; Import java.awt.*; Import java.awt.action.Actionevent; Import Java.awt.event.actionListener; dapter; Import java.awt.event.mouseevent; Import java.awt.event.MousemotionListener;/*** uses Java to simulate the QQ desktop screenshot function*/public class test extends jframe {Private Static Final Long Serityuid = - - 267804510087895906L; Private Jbutton Button = Null; Private Jlabel Imglabel = NULL; public test () {Button = New Jbutton ("Simulation Screen (right -click exit)"); Button.AdDactionListener (New ActionListener () {PUBLIC VOID ActionPerFormed (ActionEvent E) {Try {New Screenwindow (Imglabel);} Catch (Exception E1) {jobpane.showconfirmDialog (null, "accidental error!", "System prompt", jobpane.default_opting, jobpane.error_message);}}}); el (); Pane.setgroundground (color.white );; Pane.add (Imglabel); jscrollpane spane = new jscrollpane (Pane); this.GetContentPane (). Add (Button, Borderlayout.north) ; this.getContentPane (). add (spane) ; this.setDefaultCloseoperation (jframe.exit_on_close); this.Setsize (300, 200); this.setLocationRelationto (null) ;Setvisible (True); } Public Static void main (string [] args) {new test (); }} Class Screenwindow Extends JFRAME {Private Static Final Long SerialVersionuid = -3758062802950480258L; Private Boolean isdrag = False Inton x = 0; Private Int y = 0; Private int xed = 0; Private int Yend = 0; Public Screenwindow (FINAL JLABEL Imglabel) Throws Awtexception, InterruptedException {Dimension ScreenIs = Toolkit.getDefaultToolkit (). W JLABEL (New Imageicon (Screenimage.getScreenimage (0, 0, ScreenDims.width, ScreenDims.Height)); Label .setcursor (New Cursor (Cursor.crosshair_cursor)); label.addmouselistener (new mouseadapter () {Public Void MouseClick (mouseevent E) {if) Button () == mouseeEvent.button3) {dispose ();}} public void mousepRessed (mouseeEvent E) {x = e.getx (); y = e.get ();} Public void mousereleased (mouseeEvent E) {if (ISDRAG) {xend = e.getx (); End = e.gety (); if (x> xed) {int Temp = x; x = xed; xed = Temp;} if (y> ynd) {int Temp = y; y = ynd; ynd = temp;} try {imglabel.Seticon (New Imageicon (ScreenImage.GetScreenimage (x, y, xend -x, ynd -y));} Catch (Exception EX) {jobpane.showconfirmdialog (null, "accidental error! "," System Tips ", jobpane.default_opting, jobpane.error_message);} Dispose ();}}); Label.addmousemotionListener (New MousemotionListener () { Public void mouseDragaged (mouseeEvent E) {if (! isdrag) isdrag = True;} Public Void MouseMoved (MouseEvent E) { / ** Drag the Drag Line Selection Fringeer needs to be implemented by itself* /}); this.SetundecoRated (true); this.getContentPane (). Add (label ); this. Setsize (ScreenDims.Width, ScreenDims.Height); this.Setvisible (true); this.SetextendedState (jframe.maximized_both); {PUBLIC Static Image GetScreenimage (int x, int y, int W, int H) Throws AWTexception, InterruptedException {Robot Robot = New Robot (); Image Screen = Robot.createScreenCapture (New RECTANGLE (x, Y, H)). (W, h, image.scale_smooth); Mediaatracker tracker = new Mediaatracker (New Label ()); Tracker.addimage (screen, 1); Tracker.waitforid (0); Return screen;}}
It is hoped that this article is helpful to everyone's Java program design.