📄 croptest.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.Applet;
/*类CropTest是Applet的子类,该类实现了MouseListener接口,目的是实现图像的剪切*/
public class CropTest extends Applet implements MouseListener{
/*声明一个Image对象oldimgObj,用于存放加载的原始图像*/
Image oldimgObj;
/*声明一个Image对象newimgObj,用于存放处理后的图像*/
Image newimgObj;
/*声明两个整型变量x和y,用于获得鼠标单击的位置坐标*/
int x,y;
/*声明一个Graphics对象*/
Graphics g = getGraphics();
/*定义几个整型变量,用于图像的处理,x1、y1分别代表剪切图像的左上角的横纵坐标,width和height代表剪切图像的大小*/
int x1,y1,width,height;
public void init(){
/*获得载入的图像对象,赋给oldimgObj对象*/
oldimgObj = getImage(getCodeBase(),"a014.jpg");
/*让newimgObj刚开始也指向载入的图像对象*/
newimgObj = oldimgObj;
/*为该程序类所在的Applet注册鼠标时间监听器*/
addMouseListener(this);
}
/*paint方法用于将图像显示出来*/
public void paint(Graphics g){
/*获得处理后图像的宽度*/
int width = newimgObj.getWidth(this);
/*获得处理后图像的高度*/
int height = newimgObj.getHeight(this);
/*获得当前窗口的大小*/
Dimension mensionObj = getSize();
/*下面的两个语句根据图像大小和窗口大小的比较,来决定width和height变量的取值*/
if(width>mensionObj.width) width = mensionObj.width;
if(height>mensionObj.height) height = mensionObj.height;
//g.drawImage(newimgObj,(mensionObj.width-width)/2,(mensionObj.height-height)/2,this);
g.drawImage(newimgObj,0,0,this);
}
/*下面的几个方法实现MouseListener接口*/
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
/*重写mousePressed方法,获得鼠标按下时的位置*/
public void mousePressed(MouseEvent e){
/*将获得的鼠标单击位置的横纵坐标分别赋给x和y*/
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e){
/*获得鼠标松开时的位置*/
x1 = e.getX();
y1 = e.getY();
/*下面的四行语句根据鼠标两次单击的位置来确定图像的width和height*/
if(x1>x) width = x1-x;
else width = x-x1;
if(y1>y) height = y1-y;
else height = y-y1;
ImageFilter filterObj = new CropImageFilter(x,y,width,height);
/*根据提取的图像建立新的图像数据*/
ImageProducer producerObj = new FilteredImageSource(oldimgObj.getSource(),filterObj);
/*生成新的图像对象,并将该对象返回*/
newimgObj = createImage(producerObj);
repaint();
oldimgObj = newimgObj;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -