Java screenshot Similar to QQ screen capture

Source: Internet
Author: User
Tags gety

Package com.sun.test;

Import java.awt.AWTException;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.EventQueue;
Import Java.awt.FlowLayout;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseMotionAdapter;
Import Java.awt.image.BufferedImage;
Import java.awt.image.RescaleOp;
Import Java.io.File;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JToolBar;
Import Javax.swing.JWindow;
Import Javax.swing.filechooser.FileNameExtensionFilter;
Import Javax.swing.filechooser.FileSystemView;


public class Screenshottest {
public static void Main (string[] args) {

Eventqueue.invokelater (New Runnable () {
@Override
public void Run () {
try {
Screenshotwindow ssw=new Screenshotwindow ();
Ssw.setvisible (TRUE);
} catch (Awtexception e) {
E.printstacktrace ();
}
}
});
}

}
/*
* Window
*/
Class Screenshotwindow extends JWindow
{
private int orgx, orgy, endx, Endy;
Private BufferedImage Image=null;
Private BufferedImage Tempimage=null;
Private BufferedImage Saveimage=null;

Private Toolswindow Tools=null;

Public Screenshotwindow () throws awtexception{
Get screen Size
Dimension d = Toolkit.getdefaulttoolkit (). Getscreensize ();
This.setbounds (0, 0, d.width, d.height);

Capture Screen
Robot Robot = new Robot ();
Image = Robot.createscreencapture (new Rectangle (0, 0, d.width,d.height));

This.addmouselistener (New Mouseadapter () {
@Override
public void mousepressed (MouseEvent e) {
Record the end point coordinates when the mouse is released and hide the action window
ORGX = E.getx ();
Orgy = E.gety ();

if (tools!=null) {
Tools.setvisible (FALSE);
}
}
@Override
public void mousereleased (MouseEvent e) {
When the mouse is released, the Action window is displayed
if (tools==null) {
Tools=new Toolswindow (Screenshotwindow.this,e.getx (), e.gety ());
}else{
Tools.setlocation (E.getx (), e.gety ());
}
Tools.setvisible (TRUE);
Tools.tofront ();
}
});

This.addmousemotionlistener (New Mousemotionadapter () {

@Override
public void mousedragged (MouseEvent e) {
Record coordinates and redraw windows while dragging the mouse
EndX = E.getx ();
Endy = E.gety ();

Temporary image to buffer the screen area to place the screen flashing
Image Tempimage2=createimage (ScreenShotWindow.this.getWidth (), ScreenShotWindow.this.getHeight ());
Graphics g =tempimage2.getgraphics ();
G.drawimage (tempimage, 0, 0, NULL);
int x = Math.min (ORGX, EndX);
int y = math.min (orgy, endy);
int width = math.abs (ENDX-ORGX) +1;
int height = math.abs (endy-orgy) +1;
Plus 1 to prevent width or height0
G.setcolor (Color.Blue);
G.drawrect (X-1, Y-1, width+1, height+1);
Minus 1 plus 1. Prevent the picture rectangle from covering out
SaveImage = Image.getsubimage (x, y, width, height);
G.drawimage (SaveImage, x, y, null);

ScreenShotWindow.this.getGraphics (). DrawImage (Tempimage2,0,0,screenshotwindow.this);
}
});
}

@Override
public void Paint (Graphics g) {
Rescaleop ro = new Rescaleop (0.8f, 0, NULL);
Tempimage = ro.filter (image, NULL);
G.drawimage (tempimage, 0, 0, this);
}
Save image to File
public void SaveImage () throws IOException {
JFileChooser jfc=new JFileChooser ();
Jfc.setdialogtitle ("Save");

File filter, user filter selectable files
Filenameextensionfilter filter = new Filenameextensionfilter ("JPG", "jpg");
Jfc.setfilefilter (filter);

Initialize a default file (this file is generated on the desktop)
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
String fileName = Sdf.format (New Date ());
File FilePath = Filesystemview.getfilesystemview (). Gethomedirectory ();
File Defaultfile = new file (FilePath + file.separator + fileName + ". jpg");
Jfc.setselectedfile (Defaultfile);

int flag = Jfc.showsavedialog (this);
if (flag==jfilechooser.approve_option) {
File File=jfc.getselectedfile ();
String Path=file.getpath ();
Check the file suffix, put the user forget to enter the suffix or enter an incorrect suffix
if (! ( Path.endswith (". jpg") | | Path.endswith (". JPG ")) {
path+= ". jpg";
}
Write file
Imageio.write (saveimage, "JPG", new File (path));
System.exit (0);
}
}
}
/*
* Operation window
*/
Class Toolswindow extends JWindow
{
Private Screenshotwindow parent;

Public Toolswindow (Screenshotwindow parent,int x,int y) {
This.parent=parent;
This.init ();
This.setlocation (x, y);
This.pack ();
This.setvisible (TRUE);
}

private void init () {

This.setlayout (New BorderLayout ());
JToolBar toolbar=new JToolBar ("Java");

Save button
JButton savebutton=new JButton (New ImageIcon ("Images/save.gif"));
Savebutton.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
try {
Parent.saveimage ();
} catch (IOException E1) {
E1.printstacktrace ();
}
}
});
Toolbar.add (Savebutton);

Close button
JButton closebutton=new JButton (New ImageIcon ("Images/close.gif"));
Closebutton.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
System.exit (0);
}
});
Toolbar.add (CloseButton);

This.add (Toolbar,borderlayout.north);
}

}

Java screenshot Similar to QQ screen capture

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.