Why won't my image display in my Java Applet? -
when use following code:
public void paint(graphics g){ //displays version number , name. g.setfont(new font("courier", font.plain, 10)); g.drawstring("dcoder " + execute.execute.version, 2, 10); //displays logo in center. g.drawimage(logo, centeralign(logo.getwidth(null)), 50, this); } private int width(){ //gets , returns width of applet. int width = getsize().width; return width; } private int height(){ //gets , returns height of applet. int height = getsize().height; return height; } private int centeralign(int obwidth){ int align = (width()-obwidth)/2; return align; }
in java applet, image not display until call repaint() (by resizing applet viewer window)? why won't image display?
an asynchronous loaded image has handled thus.
logo.getwidth(this); // indicate asynchronous imageobserver
...
@override public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { if ((infoflags & imageobserver.allbits) == imageobserver.allbits) { // image entirely read. repaint(); } }
when asynchronous reading image, getwidth(null)
return 0 till width determined etcetera. therefore 1 needs bit careful.
explanation
loading images designed done asynchronously. image available, before being read getwidth
and/or getheight
-1. can pass imageobserver getwidth/getheight, notified during image reading. japplet imageobserver, can pass this
.
the reading code passed/registered imageobserver's method imageupdate signal change; width known, somebits (= not all), 1 draw preview, in jpeg pixelized preview.
this asynchrone technique in earlier days of slow internet needed.
if want read image simpler, use imageio.read(...)
.
Comments
Post a Comment