📄 imageapplettracebyio.java
字号:
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
public class imageAppletTraceByIO extends Applet implements ImageObserver
{
Image img = null;
int Mx = 0;
int My = 0; //鼠标按下的位置
int width = 0;
int height = 0; //选定区域的宽和高
public void init()
{
img = getImage(getCodeBase(), "fun.jpg"); //下载图像
resize(200,200) ;
width = 100;
height = 100;
}
public boolean mouseDown(Event event, int i, int j)
{
Mx = i;
My = j; //记录鼠标按下的位置
repaint();
return true;
}
public void paint(Graphics g)
{
//由于类component实现了接口ImageObserver,所以方法drawImage
//中的ImageObserver型参数为this
g.drawImage(img, 0, 30, this); //显示原始图像
g.drawString("This is the original picture", 10,20);
if(Mx > 0)
{
int i = Math.max(0, Mx - width / 2);
int j = Math.max(0, My - height / 2); //计算左上角图标
g.copyArea(i, j, width, height, 280, 0);
g.drawString("Using copyArea method", 280, 120);
g.drawString("Using clipRect method", 430, 120); //将图像的一部分拷贝
g.clipRect(430 + i, j, width, height); //设置裁减区域
g.drawImage(img, 430, 0, this);
}
}
//重载imageUpdate()方法以实现对图形下载情况的跟踪
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height){
if((infoflags & WIDTH)==0) //图像宽度是否有效
System.out.println("width="+width+"(not available)");
else
System.out.println("width="+width+"(available)");
if((infoflags & HEIGHT)==0) //图像高度是否有效
System.out.println("height="+height+"(not available)");
else
System.out.println("height="+height+"(available)");
if ((infoflags & ALLBITS)==0){ //图像是否被完全下载
System.out.println("Image not loaded");
return true;
}
else{
System.out.println("Image is completely loaded");
repaint();
return false; //不需要继续跟踪
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -