Waiting for an image to be fully loaded?

F

Frank LaRosa

What's the right way to wait for an image to be fully loaded?

I have a non-GUI application (it's a servlet that is trying to load an
image file into memory and crop or scale it). I'm using
TookKit.createImage(byte[]) to make the image. The bytes are the
contents of a GIF or JPEG image loaded from a file.

I'm aware that images are loaded asynchronously, so I wrote some code
to use a MediaTracker to wait for the image to be fully loaded. My
code looks like this:

MediaTracker tracker = new MediaTracker(new Container());
tracker.addImage(image, 0);
if (!tracker.checkID(0, true)) {
tracker.waitForID(0);
}

Sometimes it works fine, and other times, the "waitForID" call never
returns. I can't figure out why. It works and then fails with the same
images. Also, when it fails, my CPU useage goes up to 100% and stays
there until I kill the JVM.

Is there something I need to do to initialize the graphics
environment? I'm using JDK 1.4.2_02 with the
"-Djava.awt.headless=true" option, and I'm running it on Windows with
Orion server. There is no actual window or GUI involved.

Thanks.
 
A

Andrew Thompson

....
My code looks like this:

MediaTracker tracker = new MediaTracker(new Container());
tracker.addImage(image, 0);
if (!tracker.checkID(0, true)) {
tracker.waitForID(0);
}

No Frank. That is a code _fragment_, and is probably
not very useful to those who might be able to help you..

I suggest this.

1) Come up with a short, self contained, compileable
example of what you have.

2) Read the comp.lang.java.gui FAQ, it might contain
something about image loading..

3) Read c.l.j.gui group, a lot of image related problems
are discussed there (Yeah, yeah - I know yours is 'headless',
but I still think it is worth looking into)

4) Trawl around Marc Schmidt's site, he has some
great code related to images..
http://www.geocities.com/marcoschmidt.geo/java.html

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top