java Swing怎么设置JPanel背景图片为gif动画??????背景加gif动画只能静态显 50

javaSwing怎么设置JPanel背景图片为gif动画??????背景加gif动画只能静态显示..…... java Swing怎么设置JPanel背景图片为gif动画??????背景加gif动画只能静态显示..… 展开
 我来答
编程笔记
2015-12-24 · 超过31用户采纳过TA的回答
知道答主
回答量:106
采纳率:0%
帮助的人:36万
展开全部
public class GifComponent extends JComponent {
private static final long serialVersionUID = 1L;
private GifBean[] gifBeans;
private Map<Integer, Integer[]> gifBeanMap = new HashMap<Integer, Integer[]>();
private int index = 0;
private int delayFactor;
private Timer timer;

/**
*
* @param gifFile
* @param delayFactor
* 显示gif每帧图片的时间因子
*/
public GifComponent(File gifFile, int delayFactor) {
setDelayFactor(delayFactor);
setGifFile(gifFile);
}

/**
* 设置Gif文件
*
* @param gifFile
*/
public void setGifFile(File gifFile) {
ImageReader reader = null;
try {
ImageInputStream imageIn = ImageIO.createImageInputStream(gifFile);
Iterator<ImageReader> iter = ImageIO
.getImageReadersByFormatName("gif");
if (iter.hasNext()) {
reader = iter.next();
}
reader.setInput(imageIn, false);
gifBeanMap.clear();
gifBeans = new GifBean[reader.getNumImages(true)];
GIFImageMetadata meta = null;
for (int i = 0; i < gifBeans.length; i++) {
meta = (GIFImageMetadata) reader.getImageMetadata(i);
gifBeans[i] = new GifBean();
gifBeans[i].image = reader.read(i);
gifBeans[i].x = meta.imageLeftPosition;
gifBeans[i].y = meta.imageTopPosition;
gifBeans[i].width = meta.imageWidth;
gifBeans[i].height = meta.imageHeight;
gifBeans[i].disposalMethod = meta.disposalMethod;
gifBeans[i].delayTime = meta.delayTime == 0 ? 1
: meta.delayTime;
}
for (int i = 1; i < gifBeans.length; i++) {
if (gifBeans[i].disposalMethod == 2) {
gifBeanMap.put(new Integer(i), new Integer[] { i });
continue;
}
int firstIndex = getFirstIndex(i);
List<Integer> list = new ArrayList<Integer>();
for (int j = firstIndex; j <= i; j++) {
list.add(j);
}
gifBeanMap.put(new Integer(i), list.toArray(new Integer[] {}));
}
} catch (IOException e) {
e.printStackTrace();
}
setTimer();
}

private synchronized void setTimer() {
if (timer != null) {
timer.cancel();
}
timer = new Timer("show gif");
timer.schedule(new TimerTask() {
@Override
public void run() {
repaint();
try {
Thread.sleep(gifBeans[index].delayTime * delayFactor);
} catch (InterruptedException e) {
}
index++;
if (index >= gifBeans.length) {
index = 0;
}
}

}, 0, 1);

}

/**
* 设置时间因子
*
* @param delayFactor
*/
public void setDelayFactor(int delayFactor) {
this.delayFactor = delayFactor;
}

@Override
protected void paintComponent(Graphics g) {
g.drawImage(gifBeans[0].image, gifBeans[0].x, gifBeans[0].y, this);
if (index > 0) {
Integer[] array = gifBeanMap.get(index);
for (Integer i : array) {
g.drawImage(gifBeans[i].image, gifBeans[i].x, gifBeans[i].y,
this);
}
}
}

private int getFirstIndex(int index) {
int tempIndex = index;
while (tempIndex > 1) {
if (tempIndex - 1 > 0
&& gifBeans[tempIndex - 1].disposalMethod == 2) {
return index;
}
tempIndex--;
}
return tempIndex;
}

/**
* 用于保持gif每帧图片的信息
*/
public class GifBean {
public BufferedImage image;
public int x;
public int y;
public int width;
public int height;
public int disposalMethod;
public int delayTime;
}

}
追问
......
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式