分享一个在osc上看到的java版的截图软件

感觉做的还不错,给大家分享下,要是改进下是不是可以和QQ截图媲美了。 程序截图

原创不易, 转载请注明出处:分享一个在osc上看到的java版的截图软件

package com.zuidaima.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();
	}
}

	    			

 代码下载地址: http://www.zuidaima.com/share/1550463293164544.htm


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值