我曾经写过这个
JLabel label=new JLable(URL);
frame.getContentPane().add(label);
它适用于动画 gif 图像
但是,当我想使用 imageProxy 从互联网加载 gif 时,它不起作用。
我的 imageProxy 是这个
public class ImageProxy implements Icon{
ImageIcon imageIcon;
URL imageURL;
Thread retrievalThread;
boolean retrieving =false;
public ImageProxy(URL url){
imageURL = url;
}
public int getIconHeight() {skip}
public int getIconWidth() {skip}
@Override
public void paintIcon(final Component c, Graphics g, int x, int y) {
System.out.println("paint");
if(imageIcon !=null){
imageIcon.paintIcon(c, g, x, y);
}else{
g.drawString("Loading image", x+10, y+80);
if(!retrieving){
retrieving =true;
retrievalThread = new Thread(new Runnable(){
public void run(){
try{
imageIcon = new ImageIcon(imageURL);
c.repaint();
}catch(Exception e){
e.printStackTrace();
}
}
});
retrievalThread.start();
}
}
}
}
它可以成功加载图像,但它不会自动刷新它图像我必须通过缩放框架来更改图像,每次我缩放它时都会更改一张图片
我阅读了文档,它说,在其他显示 gif 时我需要 setImageObsever 我尝试过,但不起作用。和 getImageObserver 没有代理打印 null 的 imageIcon 正常工作
我也尝试阅读源代码,但我不太明白。
请帮助我,谢谢。