用鼠标点击截取一张图片的某部分

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DivImageByMouse extends JPanel{
BufferedImage image=null;
int x1,y1,x2,y2;
public DivImageByMouse(File file){
super();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
int x=x1<x2?x1:x2;
int y=y1<y2?y1:y2;
int w=(x1>x2?x1:x2)-x;
int h=(y1>y2?y1:y2)-y;
Image image=DivImageByMouse.this.getImageByClip(x, y, w, h);
setClipboardImage2(image);
x1=y1=x2=y2=0;
JOptionPane.showMessageDialog(DivImageByMouse.this,"图片已保存到系统粘贴板!","图片已保存",JOptionPane.INFORMATION_MESSAGE);
DivImageByMouse.this.repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
DivImageByMouse.this.repaint();
}
});
try {
image=ImageIO.read(file);
} catch (IOException e) {
System.out.println("输入文件不是一个图片文件!");
}
}
public Image getImage(){
return image;
}
public Image getImageByClip(int x,int y,int w,int h){
int rgbs[]=new int[w*h];
rgbs=image.getRGB(x,y, w, h, rgbs,0,w);
BufferedImage tmpImage=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
tmpImage.setRGB(0, 0, w, h, rgbs,0,w);
return tmpImage;
}
public void paint(Graphics g){
super.paint(g);
g.drawImage(image,0,0,this);
System.out.println("("+x1+","+y1+")("+x2+","+y2+")");
if(x1==0&&y1==0&&x2==0&&y2==0) return;
System.out.println("rect");
int x=x1<x2?x1:x2;
int y=y1<y2?y1:y2;
int w=(x1>x2?x1:x2)-x;
int h=(y1>y2?y1:y2)-y;
g.setColor(Color.blue);
g.drawRect(x, y, w, h);
}
protected static void setClipboardImage2(final Image image) {
Transferable trans = new Transferable(){
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(isDataFlavorSupported(flavor))
return image;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
}




public static void main(String args[]){
JFrame jf=new JFrame("");
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setBounds(50,50,1024,768);
jf.add(new DivImageByMouse(new File("e:\109.jpg")));
jf.add(new JButton(""),"North");
jf.setVisible(true);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值