Using ImageObserver instead of MediaTracker

C

crouching_buffy

Hi

I've got some code to load an image using MediaTracker, but it seems a
bit over-the-top to use this class since there's only ever one image
being loaded at any time. Can the same thing be achieved using the
ImageObserver class?

private void loadImage(Image img)
{

MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}

Ta
-Dave-
 
A

Andrew Thompson

I've got some code to load an image using MediaTracker, but it seems a
bit over-the-top to use this class since there's only ever one image
being loaded at any time.

If there is more than one image to load, your method
is inefficient.[1]
..Can the same thing be achieved using the
ImageObserver class?

ImageObserver is not a class, but an interface.
It specifies that implementing classes must define
a 'boolean imageUpdate()' method.

It is implemented by Component class and inherited
by many others. One of those might suit your purposes,
since people often load images to place in Components
or their ancestors.
private void loadImage(Image img)
{

MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}

[1] If there is more that one image, it makes more sense to
- Instantiate *every* Image (Images load asynchronously)..
- Add *every* image to the MediaTracker before you, ..
- MediaTracker.waitForAll()

[ Note: Follow-Ups set to c.l.j.help only ]

HTH
 
I

Ingo R. Homann

Hi,

Hi

I've got some code to load an image using MediaTracker, but it seems a
bit over-the-top to use this class since there's only ever one image
being loaded at any time. Can the same thing be achieved using the
ImageObserver class?

private void loadImage(Image img)
{

MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}

If you do not want to do something special if the image is loaded to a
certain degree, I think the MediaTracker is exactly what you need!?

Ciao,
Ingo
 
K

Knute Johnson

Hi

I've got some code to load an image using MediaTracker, but it seems a
bit over-the-top to use this class since there's only ever one image
being loaded at any time. Can the same thing be achieved using the
ImageObserver class?


Ta
-Dave-

Yes. It works just fine.

knute...
email s/nospam/knute/
 
C

crouching_buffy

OK, thanks everyone - I might just stick with the MediaTracker then, in
the meantime.

Ta v much
-Dave-
 
R

Roedy Green

I've got some code to load an image using MediaTracker, but it seems a
bit over-the-top to use this class since there's only ever one image
being loaded at any time. Can the same thing be achieved using the
ImageObserver class?

private void loadImage(Image img)
{

MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}

Just look at the code for MediaTracker and see just how much you save
by making it handle only a single Image.

Such an "optimisation" is likely foolish for several reasons:

1. you have not identified MediaTracker as a bottleneck.

2. Your code will baffle others maintaining your code who are
expecting a MediaTracker.

3. You will chew up RAM for two classes when one would have done.

However, as an educational project to understand ImageObservers, go
ahead. Extreme curiosity is the only thing that keeps programmers sane
when confronted with other people's (e.g. Sun's, Microsoft's) code.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top