We help IT Professionals succeed at work.

image display problem

Holger101497 asked
Medium Priority
Last Modified: 2010-03-30
Who can tell me the "proper" way of loading an image from the web and displaying it in a component?

I (am trying to) write a component that displays (some) HTML and need images (please don't tell me there are JavaBeans for displaying HTML - it's really hard to find a JavaBean for JDK 1.02 *g* *sigh*). I only need basic functionality for certain pages (under my control).

The component is derived from a Panel (display area + scrollbar(s)). I use theApplet.getImage(...) to get the image and "offscreenGraphics.drawImage(img,x,y,width,height,theApplet)" to display it (sorry, don't have the exact code here, but it would really help if I had some answer before Monday morning...

The code is "kind of" correct, but I seem to have a problem with the "asynchronous retrieval". The first time I try to display an image, it doesn't show, the second (and subsequent) times (for the same image), it works fine.

The help file says: getImage returns an image object immediately, even if the image is not yet available. The image is retrieved as soon as it is needed. Well, this doesn't seem to work.
What exactly is the role of the "imageObserver" that is passed as a parameter. What does it need to do?? I currently only pass the applet (which worked before). Right now, I use a rather simply method for layout and it would be great if I could have the image available at a known time instead of havin to respond to something like an "imageComplete"-event (i.e. save the positions of all images).

I'll increase the points for good answers that do not only solve the problem but also tell me WHY it doesn't work right now. I could probably find a workaround myself, but that's NOT what I'm looking for - the code needs to be 100% reliable!

Thanks for your help! :-))
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


Thanks, I'll take a closer look at the MediaTracker - what's the best way of getting the dimensions of an image right away to continue with the page and fill in the image later?
The MediaTracker seems to tell me when the image is "complete", but not when I know about the size...

Hi 'Holger',

Because of the specific nature of the Internet connections (slow, fast, unreliable, reliable, etc.) although the class Image has the parameters, they might return -1 if the information about the image hasn't arrived yet. When the mediatracker says the image is complete, you can obtain the dimensions from the Image object. But this is maybe too late for you.
Usually this information is in a header, but imagine you're receiving streaming data which means you will know the size when it all arrives. With the Image it is not so complicated:
If you wait for loading one particular picture and then call the getHeight() of the Image object it will return the right number.
But this means waiting the whole image. A little bit earlier, I believe after the image header arrives, the scenario with Image observers will work. With a ImageObserver object you can observe the process of loading the image more closely. As far as I remember you can get notified even about such things like another frame from the same animated image has arrived. When you say getWith() and register a custom ImageObserver, then when the information becomes available, the updateImage() method of the so registered observer will get notified. Then you can test the flags and if the HEIGHT and WIDTH flags are raised this means you can query the height/width immediately. If not yet, the the method returns false and does get notified if another change of the status appears.



oooops... sorry sorry sorry - I completely forgot about this question --- that's very sad considering how mad I am about people asking questions without checking or grading the (i.e. my) answers *ggg*.

Good thing this has not yet been auto-graded as a C, because it solved my problem. I had kind of figured it out by myself, but that doesn't make the answer any less correct or helpful.

I did use the ImageObserver and it works like you described it. JDK, Netscape and IE each have their own opinion about when to send which information >:-|, but with some experimenting, I found a way to make everything work :-))

P.S.: I added a few points as an apology for the delay (I have some many lying around on my desk *g* - just as you, I mostly answer questions ;-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.