import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CopyArea {
JFrame frame;
public CopyArea(){
frame=new JFrame();
frame.setSize(500, 500);
frame.add(new DrawPanel());
frame.setVisible(true);
}
public static void main(String arg[]){
new CopyArea();
}
}
class DrawPanel extends JPanel implements MouseListener,MouseMotionListener
{
int x1;
int y1;
int x2;
int y2;
int xx1;
int yy1;
int xx2;
int yy2;
Color color;
int sign=0;
public DrawPanel()
{
setBackground(Color.white);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
if(sign==0){
x1=e.getX();
y1=e.getY();
}
else{
xx1=e.getX();
yy1=e.getY();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
ImageIcon imageIcon = new ImageIcon("pic.jpg");
Image im = imageIcon.getImage();
g.drawImage(im, 0, 0, this);
}
public void mouseReleased(MouseEvent e)
{if(sign==0){
x2=e.getX();
y2=e.getY();
sign=(sign+1)%2;
} else{
xx2=e.getX();
yy2=e.getY();
Graphics g=this.getGraphics();
g.copyArea(Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2)-Math.min(x1, x2), Math.max(y1, y2)-Math.min(y1, y2), xx1-x1, yy1-y1);
// System.out.println("("+x1+","+y1+")"+"("+x2+","+y2+")"+"("+xx1+","+yy1+")");
sign=(sign+1)%2;
}
}
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseDragged(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{}
}
java 复制图像
需积分: 3 116 浏览量
2008-07-08
09:27:46
上传
评论 1
收藏 17KB RAR 举报
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
o936753
- 粉丝: 0
- 资源: 10
最新资源
- 数据库课程设计on-model-for-netwo开发笔记
- linux常用命令大全mage-processing-m开发笔记
- mysql安装配置教程droid-m开发笔记
- PWMmodel-for-network-ids-ma开发笔记
- apache-doris-build-env-for-2.0.c
- apache-doris-build-env-for-2.0.b
- apache-doris-build-env-for-2.0.a
- 1719422999141832_lls-release_cqfw.mobileconfig
- 数据库课程设计processing-m开发笔记
- pycharm安装教程ention-model-for-开发笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0