📄 specialimage.java
字号:
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
/** 这是一个显示图像的JApplet,图像一张一张地被显示出来,
* 效果是下一张慢慢地从中间扩大,直到将前一张覆盖住 */
/** JApplet类 */
public class SpecialImage extends JApplet
{
private Thread runner;
private Image image[];
private BufferedImage showImage;
private int currentImage,nextImage;
private int imageCount;
private int imageWidth;
private int imageHeight;
private String imageName;
private ImagePanel p;
private Graphics2D g2;
/** init()方法 */
public void init()
{
try
{
String param=getParameter("imagecount");//取得图像文件数
if(param!=null)
imageCount=Integer.parseInt(param);
else
throw new Exception();
image=new Image[imageCount];
for(int i=0;i<imageCount;i++)
{
imageName=getParameter("image"+(i+1));//得到每一图像文件名
if (imageName==null) throw new Exception();
loadImage(i);//载入每一图像
}
}
catch(Exception e)
{
showStatus("Error: "+e);
}
imageWidth=image[0].getWidth(this);
imageHeight=image[0].getHeight(this);
currentImage=0;
showImage= new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);// 生成图像的后台缓冲
g2=showImage.createGraphics();//获取在缓冲区做图的环境
g2.drawImage(image[0],0,0,null);//完成第一张图的后台图像的绘制
p=new ImagePanel();
setContentPane(p);
runner=new Thread(p);
runner.start();
}//init()方法结束
/** 该方法用来载入图象 */
public void loadImage(int i)
{
try
{
URL url=new URL(getDocumentBase(),imageName);
image[i]=getImage(url);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image[i],0);
tracker.waitForID(0);
}
catch(InterruptedException e)
{
showStatus("Loading interrupted");
}
catch(MalformedURLException e)
{
showStatus("Bad URL");
}
}//loadImage()方法结束
/** JApplet的start()方法 */
public void start()
{//浏览器返回页面时恢复动画线程
if (runner==null)
{
runner=new Thread(p);
runner.start();
}
}
/** JApplet的stop()方法 */
public void stop()
{//浏览器离开页面时取消动画线程
runner.interrupt();
runner=null;
}
/** 面板内部类,在JApplet的init()方法中被置为内容面板 */
public class ImagePanel extends JPanel implements Runnable
{
/** 面板组件的paintComponent()方法 */
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(showImage==null) return;
g.drawImage(showImage,0,0,null);
//在面板上绘制已准备好的后台图像
}
/** 在线程中显示动画 */
public void run()
{
while (!Thread.interrupted())
{
nextImage=(currentImage+1)%imageCount;
try
{
Thread.sleep(1000);
for(int x=imageWidth/2,y=imageHeight/2,i=0;x>(-imageWidth/10);x-=imageWidth/10,y-=imageHeight/10,i++)
{
g2.drawImage(image[nextImage],x,y,2*(imageWidth*i)/10,2*(imageHeight*i)/10,null);
//完成下一张图的后台图像的绘制,为循环特效显示
repaint();//在面板上重绘已准备好的后台图像
Thread.sleep(100);
}
currentImage=nextImage;
}
catch(InterruptedException e)
{}
}
}//run()方法结束
}//ImagePanel内部类结束
}//SpecialImage类定义结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -