This article describes the Java simulation QQ Desktop screenshot function realization method. Share to everyone for your reference. Specifically as follows:
QQ Desktop screenshot function is very convenient, last year used to simulate a Java, is sorted out.
This method first needs to catch the entire screen image, the image displayed in a jframe, and then the JFrame full-screen display, so that the simulation of a desktop, Java can also get the role of the mouse to achieve a small area of the desktop screen screenshots.
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseMotionListener; /** * Use Java to simulate the QQ desktop screenshot function/public class Test extends JFrame {private static final long Serialversionuid =-267804510087
895906L;
Private JButton button = null;
Private JLabel Imglabel = null;
The public Test () {button = new JButton ("The Analog screen (right click Exit)"); Button.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {try {new Screenwindow
(Imglabel); catch (Exception E1) {Joptionpane.showconfirmdialog (NULL, unexpected error!)
"," System hint ", joptionpane.default_option, Joptionpane.error_message);
}
}
});
JPanel pane = new JPanel ();
Pane.setbackground (Color.White);
Imglabel = new JLabel ();
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.setlocationrelativeto (NULL);
This.setvisible (TRUE);
public static void Main (string[] args) {new Test ();
} class Screenwindow extends JFrame {private static final long serialversionuid = -3758062802950480258l;
Private Boolean Isdrag = false;
private int x = 0;
private int y = 0;
private int xend = 0;
private int yend = 0; Public Screenwindow (final JLabel Imglabel) throws Awtexception, interruptedexception {Dimension screendims = Toolkit.get
Defaulttoolkit (). Getscreensize ();
JLabel label = new JLabel (New ImageIcon (screenimage.getscreenimage (0, 0, screendims.width, screendims.height));
Label.setcursor (New Cursor (cursor.crosshair_cursor)); Label.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent e) {if (E.getbutton () = = mouseevent.b
UTTON3) {Dispose ();
} public void mousepressed (MouseEvent e) {x = E.getx ();
y = e.gety ();
}public void mousereleased (MouseEvent e) {if (Isdrag) {xend = E.getx ();
Yend = E.gety ();
if (x > xend) {int temp = x;
x = xend;
Xend = temp;
} if (Y > yend) {int temp = y;
y = yend;
Yend = temp;
try {Imglabel.seticon (New ImageIcon (Screenimage.getscreenimage (x, Y, Xend-x, yend-y)); ' Catch (Exception ex) {Joptionpane.showconfirmdialog (NULL, unexpected error!)
"," System hint ", joptionpane.default_option, Joptionpane.error_message);
Dispose ();
}
}
}); Label.addmousemotionlistener (New Mousemotionlistener () {public void mousedragged (MouseEvent e) {if (!isdrag) IsDra
G = true;
public void mousemoved (MouseEvent e) {/** The dotted selection box for the drag process 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); } class Screenimage {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, W, h)). getScaledInstance (W, H, Image.scale_smooth);
Mediatracker tracker = new Mediatracker (new Label ());
Tracker.addimage (screen, 1);
Tracker.waitforid (0);
return screen;
}
}
I hope this article will help you with your Java programming.