Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


image display problem

Posted on 1998-09-05
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! :-))
Question by:Holger101497
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Accepted Solution

diakov earned 280 total points
ID: 1223633
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.



Author Comment

ID: 1223634
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...

Expert Comment

ID: 1223635
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.


Author Comment

ID: 1223636
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 ;-)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question