码迷,mamicode.com
首页 > 编程语言 > 详细

用java写一个截图工具

时间:2015-09-20 13:03:38      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

用java模仿qq截图

首先定义一个启动类 ,运行这个方法

 1 import java.awt.Rectangle;
 2 import java.awt.Robot;
 3 import java.awt.Toolkit;
 4     
 5     public class screen {
 6         
 7         public static void main(String[] args) throws Exception {
 8             int width=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
 9             int height=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
10             Robot r=new Robot();
11             new View(r.createScreenCapture(new Rectangle(0, 0, width, height)));
12         }
13     }
14     

初始化截屏

import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class View extends JFrame {
	BufferedImage image;
	BufferedImage srcimage;
	JPanel jp;
	ImageIcon icon;

	View(BufferedImage image) {

		int width = (int) Toolkit.getDefaultToolkit().getScreenSize()
				.getWidth();
		int height = (int) Toolkit.getDefaultToolkit().getScreenSize()
				.getHeight();
		this.image = image;
		icon = new ImageIcon(image);
		jp = new huabu(image);
		this.add(jp);
		this.setSize(width, height);
		this.setUndecorated(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

}

  鼠标监听事件

  1 import java.awt.Color;
  2 import java.awt.Graphics;
  3 import java.awt.event.ActionEvent;
  4 import java.awt.event.ActionListener;
  5 import java.awt.event.MouseEvent;
  6 import java.awt.event.MouseListener;
  7 import java.awt.event.MouseMotionListener;
  8 import java.awt.image.BufferedImage;
  9 import java.io.File;
 10 import java.io.FileOutputStream;
 11 import java.util.Date;
 12 
 13 import javax.imageio.ImageIO;
 14 import javax.swing.JButton;
 15 import javax.swing.JOptionPane;
 16 import javax.swing.JPanel;
 17 
 18 public class huabu extends JPanel implements MouseListener,
 19         MouseMotionListener, ActionListener {
 20     BufferedImage image;
 21     int sx = 0;
 22     int sy = 0;
 23     int tx = 0;
 24     int ty = 0;
 25     boolean flag = true;
 26     JButton save = new JButton("保存");
 27 
 28     huabu(BufferedImage image) {
 29         this.setLayout(null);
 30         this.image = image;
 31         this.addMouseListener(this);
 32         this.addMouseMotionListener(this);
 33         save.addActionListener(this);
 34     }
 35 
 36     @Override
 37     public void paint(Graphics g) {
 38         // TODO Auto-generated method stub
 39         g.drawImage(image, 0, 0, null);
 40         draw(g);
 41 
 42     }
 43 
 44     private void draw(Graphics g) {
 45         g.setColor(Color.cyan);
 46         g.drawLine(sx, sy, tx, sy);
 47         g.drawLine(sx, ty, tx, ty);
 48         g.drawLine(sx, sy, sx, ty);
 49         g.drawLine(tx, sy, tx, ty);
 50         System.out.println("画出" + tx + " " + ty);
 51     }
 52 
 53     @Override
 54     public void mouseDragged(MouseEvent e) {
 55 
 56         tx = e.getX();
 57         ty = e.getY();
 58         this.repaint();
 59         System.out.println("改变目标" + tx + " " + ty);
 60 
 61     }
 62 
 63     @Override
 64     public void mouseMoved(MouseEvent e) {
 65         // TODO Auto-generated method stub
 66 
 67     }
 68 
 69     @Override
 70     public void mouseClicked(MouseEvent e) {
 71 
 72     }
 73 
 74     @Override
 75     public void mousePressed(MouseEvent e) {
 76         if (flag) {
 77             System.out.println("改变原坐标");
 78             this.addMouseMotionListener(this);
 79             sx = e.getX();
 80             sy = e.getY();
 81         }
 82     }
 83 
 84     @Override
 85     public void mouseReleased(MouseEvent e) {
 86         flag = false;
 87         int x = tx > sx ? tx : sx;
 88         int y = ty > sy ? ty : sy;
 89         save.setBounds(x - 100, y + 3, 100, 30);
 90         this.add(save);
 91         save.setVisible(true);
 92         save.requestFocus();
 93         this.repaint();
 94 
 95     }
 96 
 97     @Override
 98     public void mouseEntered(MouseEvent e) {
 99         // TODO Auto-generated method stub
100 
101     }
102 
103     @Override
104     public void mouseExited(MouseEvent e) {
105         // TODO Auto-generated method stub
106 
107     }
108 
109     @Override
110     public void actionPerformed(ActionEvent e) {
111         if (e.getSource() == save) {
112             String name = new Date().getTime() + "";
113             File f = new File("D:\\" + name + ".jpg");
114             FileOutputStream out;
115             try {
116                 out = new FileOutputStream(f);
117                 System.out.println("保存" + tx + " " + ty);
118 
119                 if (sx > tx) {
120                     exchange(sx, tx);
121                 }
122                 if (sy > ty) {
123                     exchange(sy, ty);
124                 }
125                 ImageIO.write(image.getSubimage(sx, sy, tx - sx, ty - sy),
126                         "jpg", out);
127                 out.close();
128                 JOptionPane.showMessageDialog(null, "图片" + name + "成功保存到D:盘");
129                 System.exit(1);
130             } catch (Exception e1) {
131                 e1.printStackTrace();
132             }
133         }
134     }
135 
136     private void exchange(int sy2, int ty2) {
137         int temp = sy2;
138         sy2 = ty2;
139         ty2 = temp;
140     }
141 
142 }

 

用java写一个截图工具

标签:

原文地址:http://www.cnblogs.com/jyyjava/p/4823141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!