Java implementation of any position screenshot (imitation qq screenshot) __java

Source: Internet
Author: User
Tags gety

Just finished learning Java recently, I did this small program. Of course, also through Baidu to find information, and finally made this small program.

Function:

Click on the screenshot button, start the screen, the screen to draw an area, you can double-click to save the picture to the desktop. If you do not want to intercept the screen, right mouse, exit the program.

Mine is the WIN10 system, so the address of the desktop is: C:\\users\\administrator\\desktop.

This applet can be used to achieve the upper left or lower right corner of the screenshot.

This small program, every time the screen, quit the program, the ability is limited, so want to screen again, you need to open again.

The following is the code:

Import Javax.imageio.ImageIO;
Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
Import java.awt.image.*;
Import java.io.*;
	public class Drawrecttest {public static void main (string[] args) {new newframe ();
	} class Newframe extends JFrame {private static final long serialversionuid = 1L; * * Create a small window.
	 Click on the button to intercept the screen.
	* * JButton button;
		Newframe () {setvisible (true);
		SetLayout (New FlowLayout ());
		SetBounds (1000, 600, 100, 100);
		Setresizable (FALSE);
		button = new JButton ("screenshot");
		Add (button); Button.addactionlistener (new ActionListener () {//mouse click button, new one screenframe, set visible, public void actionperformed (actio
						Nevent e) {screenframe SF = new Screenframe ();
						Sf.setalwaysontop (TRUE);
						Sf.setundecorated (TRUE);
					Sf.setvisible (TRUE);
		}
				});
					Addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);
	}
				}); } class Screenframe extends JFrame {PRIvate static final Long serialversionuid = 2L;
	 * * Create a full-screen window that places the full screen image on the JFrame window for screenshots.
	* * Dimension di = Toolkit.getdefaulttoolkit (). Getscreensize ();
		Screenframe () {//setting size, that is, Full-screen setSize (DI);
	Returns the ContentPane object Getcontentpane (). Add (New DrawRect ()) for this form; Class DrawRect extends JPanel implements Mousemotionlistener, MouseListener {private static final long Serialversi
		Onuid = 3L;
		* * The full screen of the image on the JPanel, you can get the JPanel through the new DrawRect, and JPanel has full-screen image/int sx, SY, ex, EY;
		int count = 1;
		File file = null;
		BufferedImage image, GetImage;
		Boolean flag = true;
				Public DrawRect () {try {//Get Full-screen image data, return to image Robot Robot = new Robot ();
			Image = Robot.createscreencapture (new Rectangle (0, 0, di.width, di.height));
			catch (Exception e) {throw new RuntimeException ("screenshot failed");
			}//Add mouse Activity Event Addmouselistener (this);
		Addmousemotionlistener (this);
	}//Rewrite paintcomponent, display the range of screenshots by repaint public void Paintcomponent (Graphics g)	{g.drawimage (image, 0, 0, di.width, di.height, this);
			G.setcolor (Color.Blue);
			if (SX < ex && sy < EY)//Lower right corner g.drawrect (SX, SY, EX-SX, Ey-sy);
		else//upper left corner G.drawrect (ex, EY, Sx-ex, Sy-ey); The following are mouse events public void mouseclicked (MouseEvent e) {if (E.getbutton () = = Mouseevent.button3)//Right-click Exit program System.
			Exit (0);
				else if (e.getclickcount () = = 2)//Double-click screenshot {try {cutscreens ();
				catch (Exception ex) {ex.printstacktrace ();
			The custom screen function private void Cutscreens () throws Exception {Robot ro = new Robot ();
			if (SX < ex && sy < EY)//lower right corner getImage = ro.createscreencapture (new Rectangle (SX, SY, EX-SX, Ey-sy));
			else//upper left corner getImage = ro.createscreencapture (New Rectangle (ex, EY, Sx-ex, Sy-ey));
			String name = "Jietu" + Count + ". bmp";
			File = new file ("C:\\users\\administrator\\desktop", name);
			while (File.exists ()) {	String na = "Jietu" + (count++) + ". bmp";
			File = new file ("C:\\users\\administrator\\desktop", NA);
			} imageio.write (GetImage, "BMP", file);
		System.exit (0);
				The public void mousepressed (MouseEvent e) {if (flag) {SX = E.getx ();
			sy = e.gety ();
		} public void mousereleased (MouseEvent e) {flag = false; The public void mouseentered (MouseEvent e) {} is public void mouseexited (MouseEvent e) {}//mouse move, drawing the range of the screen to be truncated by repaint PU
			Blic void mousedragged (MouseEvent e) {ex = E.getx ();
			ey = E.gety ();
		Repaint ();
 } public void mousemoved (MouseEvent e) {}}}


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.