knowing when images have been loaded. (mediatracker not suitable)

N

nntpman68

Hi,

I'd like to load N (3 < N < 100) images from an applet.

In order to have a non blocking applet UI I'd like to load all images
from a separate thread.

Every time a new image has been completely loaded I would like to be
notified in order to do some processing in in order to redraw.

The Mediatracker is not suitable, as I
can either check fro ALL images to be loaded or I could wait for a
certain image to be loaded.


What I wanted is to be notified for any image (out of a certain list|)
to be completed.

With Mediatracker I would need N threads to achieve this.

What is the recommended way to achieve this?

thanks in advance for any suggestions


n
 
K

Knute Johnson

nntpman68 said:
Hi,

I'd like to load N (3 < N < 100) images from an applet.

In order to have a non blocking applet UI I'd like to load all images
from a separate thread.

Every time a new image has been completely loaded I would like to be
notified in order to do some processing in in order to redraw.

The Mediatracker is not suitable, as I
can either check fro ALL images to be loaded or I could wait for a
certain image to be loaded.


What I wanted is to be notified for any image (out of a certain list|)
to be completed.

With Mediatracker I would need N threads to achieve this.

What is the recommended way to achieve this?

thanks in advance for any suggestions


n

The two simplest ways are for you to check to see if the image is loaded
or wait for it to load. If that is not workable and you need to be
notified, override ImageObserver.imageUpdate() to call a method or fire
an event when the image is completely loaded.

Why exactly do you need to know that the images have loaded? There
might be some simpler way to handle your problem.
 
M

Mark Space

nntpman68 said:
In order to have a non blocking applet UI I'd like to load all images
from a separate thread.

As Knute alluded there are probably better ways of doing this.

First, all images are already loaded asynchronously. If you use
getImage() it returns immediately, and loads the image in the background.

URL myImageUrl = new URL( "http://myserver/images/myImage.jpg" );
Image myImage = java.awt.Toolkit.getImage( myImageUrl );

The java.awt.Component class implements the ImageObserver interface.
That means all your components (JButtons, Applets, JPanes, JPanels,
etc.) can act as their own image observer. The default behavior is to
call your components repaint() method if the image does not get fully
drawn. There's a default limit of 100 milliseconds in between calls so
that image loading doesn't completely consume all CPU cycles.

From inside some random component that has a custom image,

public void paint( Graphics g ) {
g.drawImage( myImage, x, y, this );
//...
}

will automatically invoke this default behavior.

NOTE 1: Nothing syntax checked.

NOTE 2: From _Learning Java_ by O'Reilly, 3rd ed., pages 709 - 710. Get
it. Buy it. Love it.
 
M

Mark Space

Mark said:
Image myImage = java.awt.Toolkit.getImage( myImageUrl );

java.awt.Toolkit is abstract. Need to call

java.awt.Toolkit.getDefaultToolkit()

to get the default one.
 
N

nntpman68

Hi Knute,

Knute said:
The two simplest ways are for you to check to see if the image is loaded
or wait for it to load. If that is not workable and you need to be
notified, override ImageObserver.imageUpdate() to call a method or fire
an event when the image is completely loaded.

Why exactly do you need to know that the images have loaded? There
might be some simpler way to handle your problem.
I think I'd like to start loading multiple images and periodically check
which images are loaded.

what is the method to find out whether an image is completely loaded?




Context:
--------
Well so far I'm just playing.
What I'd like to achieve is to load several 'tiles' and compose them
into an image and overlay them with some additional info.

It should be something like a really, really simple applet version of
Google-maps for fantasy maps.

I'd like to load 'on-screen' tiles.
Whenever a new tile finished I have to recreate a new image.

when no 'on-screem' tiles are outstanding I'd like to start loading
'off-screen' (neighbourhood of on-screen tiles) until new on-screen
tiles are needed.


n
 
K

Knute Johnson

nntpman68 said:
Hi Knute,


I think I'd like to start loading multiple images and periodically check
which images are loaded.

what is the method to find out whether an image is completely loaded?




Context:
--------
Well so far I'm just playing.
What I'd like to achieve is to load several 'tiles' and compose them
into an image and overlay them with some additional info.

It should be something like a really, really simple applet version of
Google-maps for fantasy maps.

I'd like to load 'on-screen' tiles.
Whenever a new tile finished I have to recreate a new image.

when no 'on-screem' tiles are outstanding I'd like to start loading
'off-screen' (neighbourhood of on-screen tiles) until new on-screen
tiles are needed.


n

Well in that case you probably don't really care when they are loaded,
you just want to display them when they are. You can use the
Applet.getImage() method but that won't download the image until you
draw it. I would probably use Toolkit.createImage() as I think it will
download the Image synchronously. You will probably need to put that
code in a separate thread.

If you can use a modern JRE then I would use the ImageIO classes to
download your images. The advantages are simpler code, more file types
and the end result is a BufferedImage.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top