image display problem

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! :-))
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

For tracking and image or group of images pendig for downloading you can use the MediaTracker class. It allows requesting that the data for a group of images be loaded, and waiting for a group of images to be loaded. To request that the image data for a group of images be loaded, you can use the forms of checkID() and checkAll() that take a boolean argument. Setting the           boolean argument to true starts loading the data for any images that aren't yet being loaded. Or you can request that           the image data be loaded and wait for it using the waitForID() and waitForAll() methods.

.//Where instance variables are declared:
  MediaTracker tracker;

.//In the init() method:
  tracker = new MediaTracker(this);
for (int i = 1; i <= 10; i++)
    images[i-1] = getImage(getCodeBase(),
    tracker.addImage(images[i-1], 0);
.//At the beginning of the run() method:
  //Start downloading the images. Wait until they're loaded.
catch (InterruptedException e) {}

.//At the beginning of the update() method:
//If not all the images are loaded, just clear the background
//and display a status string.
if (!tracker.checkAll()) {
  g.clearRect(0, 0, d.width, d.height);
  g.drawString("Please wait...", 0, d.height/2);
//If all images are loaded, draw.
.//same code as before...
The ImageObserver is an interface you can implement and define its  imageUpdate(Image, int, int, int, int, int).

This method is called when information about an image which was previously requested using an asynchronous interface becomes available.
So, the scenario is: If you define that your applet implements the ImageObserver and pass it the reference to the image operations that expect it, your method imageUpdate will be notified when a next portion of coming image bits has arrived, for example.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Holger101497Author Commented:
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.

Holger101497Author Commented:
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 ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.