Display animated gif in ImageIcon proxy with java swing -
i used write this
jlabel label=new jlable(url); frame.getcontentpane().add(label);
and works animated gif image
however , when want use imageproxy load gif form internet doesn't work.
my imageproxy this
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(); } } }
}
it can success load image won't auto refresh image have chage image zoom frame, every time zoom chage 1 picture
i read document , said , in other display gif need setimageobsever tried , doesn't work. , imageicon normal work no proxy print null getimageobserver
i tried read souce code did not understand.
pleas me ,thank you.
the source of problem in implementation of imageobserver
interface jlabel
:
public boolean imageupdate(image img, int infoflags, int x, int y, int w, int h) { // don't use getdisabledicon, trigger creation of icon if icon // not set. if (!isshowing() || !swingutilities.doesiconreferenceimage(geticon(), img) && !swingutilities.doesiconreferenceimage(disabledicon, img)) { return false; } return super.imageupdate(img, infoflags, x, y, w, h); }
here code swingutilities.doesiconreferenceimage
:
static boolean doesiconreferenceimage(icon icon, image image) { image iconimage = (icon != null && (icon instanceof imageicon)) ? ((imageicon)icon).getimage() : null; return (iconimage == image); }
as can see, if icon not instance of imageicon
result of imageupdate()
false
, without calling super's implementation responsible call repaint()
on jlabel
refresh new frame of animated icon. also, returning false
means no longer interested in updates.
you may extend jlabel
overcome constraint. here simple extension of jlabel
overrides imageupdate()
. actual code implementation of imageupdate
taken component.imageupdate()
, isinc
, incrate
constants simplicity:
public static class customlabel extends jlabel { private static final boolean isinc = true; private static final int incrate = 100; public customlabel(icon image) { super(image); } @override public boolean imageupdate(image img, int infoflags, int x, int y, int w, int h) { int rate = -1; if ((infoflags & (framebits | allbits)) != 0) { rate = 0; } else if ((infoflags & somebits) != 0) { if (isinc) { rate = incrate; if (rate < 0) { rate = 0; } } } if (rate >= 0) { repaint(rate, 0, 0, getwidth(), getheight()); } return (infoflags & (allbits | abort)) == 0; } }
you may plug imageupdate()
add functionality of displaying "loading image" string while image still loading.
i wonder real reason behind implementation of proxy imageicon
. if need synchronous image loading can use imageio
api.
Comments
Post a Comment