package com.image.capture;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;

import javax.swing.*;

public class Screenshot extends JFrame implements MouseMotionListener,
		MouseListener {
	private static final long serialVersionUID = 1L;
	private BufferedImage buff_img;
	private Robot robot;
	private JLabel label;
	private Point start;
	private Point end;
	private int x, y, width, height;
	private boolean over = false;
	private JLabel show_sub;
	private CaptureScreen captureScreen;
	private ImageIcon icon;
	private SubImgAction subAction;
	private JFrame ss = this;

	class SubImgAction implements MouseListener, MouseMotionListener {

		private JLabel label;
		private int x;
		private int y;
		private BufferedImage image;
		private ImageIcon icon;

		public SubImgAction(JLabel label, BufferedImage image, ImageIcon icon) {
			this.label = label;
			this.image = image;
			this.icon = icon;
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			Point dragPoint = e.getPoint();
			SwingUtilities.convertPointToScreen(dragPoint, label);
			icon.setImage(image.getSubimage(dragPoint.x - x < 0 ? 0
					: dragPoint.x - x, dragPoint.y - y < 0 ? 0 : dragPoint.y
					- y, label.getWidth(), label.getHeight()));
			label.setLocation(dragPoint.x - x, dragPoint.y - y);
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getButton() == 1 && e.getClickCount() == 2) {
				new ClipboardUtil().writeToClipboard(image.getSubimage(
						label.getX(), label.getY(), label.getWidth(),
						label.getHeight()));
				ss.dispose();
				JOptionPane.showMessageDialog(ss, "指定区域屏幕已复制到剪贴板!");
				captureScreen.setVisible(true);
			} else if (e.getButton() == 3) {
				ss.dispose();
				captureScreen.setVisible(true);
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.getButton() == 3) {
				ss.dispose();
				captureScreen.setVisible(true);
			}
			Point clickPoint = new Point(e.getPoint());
			SwingUtilities.convertPointToScreen(clickPoint, label);
			x = clickPoint.x - label.getX();
			y = clickPoint.y - label.getY();

		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub

		}

	}

	public Screenshot(CaptureScreen captureScreen) {
		this.setLayout(null);
		this.captureScreen = captureScreen;
		icon = new ImageIcon();
		this.setSize(ScreanSizeUtil.getDimension());
		setUndecorated(true);
		setExtendedState(MAXIMIZED_BOTH);
		setResizable(false);// 设置不可改变大小
		setAlwaysOnTop(true);// 设置总是在顶层(最上层)
		start = new Point();
		end = new Point();
		label = new JLabel();
		show_sub = new JLabel();
		show_sub.setIcon(icon);

		add(show_sub);
		try {
			robot = new Robot();
			buff_img = robot.createScreenCapture(new Rectangle(0, 0,
					ScreanSizeUtil.getDimension().width, ScreanSizeUtil
							.getDimension().height));// 获得整个屏幕

			subAction = new SubImgAction(show_sub, buff_img, icon);
			show_sub.addMouseListener(subAction);
			show_sub.addMouseMotionListener(subAction);
			show_sub.setBorder(BorderFactory.createLineBorder(Color.RED, 2));

			label = new JLabel() {
				private static final long serialVersionUID = 1L;

				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					g.drawImage(buff_img, 0, 0, this);
					Graphics2D g2d = (Graphics2D) g;
					g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
							RenderingHints.VALUE_ANTIALIAS_ON);
					g2d.setColor(new Color(150, 150, 150));
					AlphaComposite composite = AlphaComposite.getInstance(
							AlphaComposite.SRC_OVER, 60 / 100.0F);
					g2d.setComposite(composite);
					g2d.fill(new RoundRectangle2D.Float(0, 0, this.getWidth(),
							this.getHeight(), 0, 0));
				}
			};
			label.setBounds(0, 0, getWidth(), this.getHeight());
			add(label);
		} catch (AWTException e) {
			e.printStackTrace();
		}

		setVisible(true);
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		robot.createScreenCapture(new Rectangle(0, 0, ScreanSizeUtil
				.getDimension().width, ScreanSizeUtil.getDimension().height));
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		start.x = e.getX();
		start.y = e.getY();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		over = true;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if (!over) {
			end.x = e.getX();
			end.y = e.getY();
			x = Math.min(start.x, end.x);
			y = Math.min(start.y, end.y);
			width = Math.abs(end.x - start.x);
			height = Math.abs(end.y - start.y);
			if (width == 0 || height == 0)
				return;
			icon.setImage(buff_img.getSubimage(x, y, width, height));
			show_sub.setBounds(x, y, width, height);
			// repaint();
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		end.x = e.getX();
		end.y = e.getY();
		// repaint();
	}
}
最近下载更多
dearxo2014  LV1 2024年11月9日
fgg047  LV1 2022年1月24日
laowang933  LV1 2021年5月3日
2196316269  LV10 2020年11月21日
675104182  LV14 2020年9月22日
天空java  LV8 2020年6月1日
wngauss  LV4 2020年5月22日
SKxumeng  LV1 2019年5月28日
鱼椒盐排骨  LV8 2019年5月27日
blb555  LV14 2019年5月26日
最近浏览更多
dearxo2014  LV1 2024年11月9日
sswert  LV2 2024年9月12日
3334004690  LV10 2024年6月6日
ja12121  LV2 2023年6月17日
微信网友_5992582549164032  LV6 2023年2月21日
maoye520  LV1 2022年8月10日
fantaohaofan  LV2 2022年6月23日
977903096  LV10 2022年3月12日
fgg047  LV1 2022年1月24日
laowang933  LV1 2021年5月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友