java鼠标选取图片中间_java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)...

这是一个Java程序,创建了一个DivImageByMouse类,它继承自JPanel,允许用户通过鼠标选择图片的某个区域进行截取。用户按下鼠标时记录起点坐标,释放时获取终点坐标,然后使用getImageByClip方法裁剪指定尺寸的图片,并将其保存到系统剪贴板。程序还提供了显示和调整选取框的功能。
摘要由CSDN通过智能技术生成

DivImageByMouse是个JPanel子类,把main去掉,add到别的容器下也可以正常使用,是按照鼠标移动取的,要按200*200的取要稍微改动一下

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

int y=y1

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

int y=y1

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值