Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:
ImageIcon[] images; //用于动画的图标数组
Timer animationTimer;
int currentImage = 0; //当前图像编号
int delay = 500; //图像切换延迟
int width; //图像宽度
int height; //图像高度
public AnimatorIcon() //构造函数
{
setBackground(Color.white);
images = new ImageIcon[2]; //初始化数组
for (int i=0;iimages[i]=new ImageIcon(getClass().getResource("image"+i+".gif")); //实例化图标
width = images[0].getIconWidth(); //初始化宽度值
height = images[0].getIconHeight(); //初始化高度值
}
public void paintComponent(Graphics g) { //重载组件绘制方法
super.paintComponent(g); //调用父类函数
images[currentImage].paintIcon(this,g,70,0); //绘制图标
currentImage=(currentImage+1)%2; //更改当前图像编号
}
public void actionPerformed(Act