`

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

阅读更多
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);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics