image display problem

Posted on 1998-09-05
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
  • 2
  • 2

Accepted Solution

diakov earned 70 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
how to install java on RHEL image on EC2 4 41
spring jars download 1 35
Android development question 2 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

828 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