Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2005

Tip: Looking for answers? Try searching our database.

Using ImageObserver instead of MediaTracker

Thread view: 
crouching_buffy@hotmail.com - 15 Aug 2005 10:47 GMT
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-
Andrew Thompson - 15 Aug 2005 11:04 GMT
> 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)
>     {
[quoted text clipped - 3 lines]
>             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

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"You make connections, you win elections, but that ain't gonna be enough.."
The Angels 'City out of Control'

Knute Johnson - 16 Aug 2005 05:55 GMT
> [1] If there is more that one image, it makes more sense to
> - Instantiate *every* Image (Images load asynchronously)..
[quoted text clipped - 4 lines]
>
> HTH

Why Andrew?  I think that MediaTracker has become sort of a Java Urban
Legend.  The ImageObserver interface works just fine and you can load as
many images as you want with that method.  Not only that you don't have
to wait until they are loaded to proceed.  I think that most of the time
people just want to display the images on a Canvas or a Panel.  With
MediaTracker you change the normal behavior of loading asynchronously
and force them to load synchronously.  For what purpose?  Use the
ImageObserver and the images paint when they are available.

knute...
email s/nospam/knute/
Andrew Thompson - 16 Aug 2005 18:03 GMT
>> [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()
..
> Why Andrew?  I think that MediaTracker has become sort of a Java Urban
> Legend.  The ImageObserver interface works just fine and you can load as
> many images as you want with that method.

OK.  I'll presume you are right on that[1].

I was pretty much saying that the method the OP used
was inefficient, *presuming* they were going to stick
with the MediaTracker.

My reasoning was simply that it would be faster to add
all images to the MediaTracker before calling any of
the 'wait()' methods.

[1]  I have no reason to doubt you, you have far more
experience with images than me.

> ..With
> MediaTracker you change the normal behavior of loading asynchronously
> and force them to load synchronously.  For what purpose?  Use the
> ImageObserver and the images paint when they are available.

Having said that..  Perhaps it was a bad idea to focus on the
MediaTracker at all, especially since I do not think we ever
heard the OP say that an ImageObserver implementation was not
readily available within the ..what is it you were coding?

[ I think I've lost the thread!  :-O ]

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"You can blow out a candle, but you can't blow out a fire.  Once the flame
begin to catch, the wind will blow it higher."  Peter Gabriel 'Biko'

Knute Johnson - 16 Aug 2005 21:11 GMT
> Having said that..  Perhaps it was a bad idea to focus on the
> MediaTracker at all, especially since I do not think we ever
> heard the OP say that an ImageObserver implementation was not
> readily available within the ..what is it you were coding?
>
> [ I think I've lost the thread!  :-O ]

Andrew:

I think you have a very good point there :-).

knute...
crouching_buffy@hotmail.com - 20 Aug 2005 17:59 GMT
OK, just for the sake of my education, what would the equivalent
ImageOberver code look like?

All I am doing with the loaded image is scaling it down to create a
thumbnail for my photo app...
Knute Johnson - 21 Aug 2005 18:47 GMT
> OK, just for the sake of my education, what would the equivalent
> ImageOberver code look like?
>
> All I am doing with the loaded image is scaling it down to create a
> thumbnail for my photo app...

Scale it with the Graphics.drawImage() method.  You can load as many
images as you want.

import java.awt.*;
import java.awt.event.*;

public class test extends Canvas {
    Image img;

    public test() {
        Toolkit t = Toolkit.getDefaultToolkit();
        img = t.createImage("saturn.jpg");  // this can be a URL too!
        setPreferredSize(new Dimension(120,80));
    }

    public void paint(Graphics g) {
        g.drawImage(img,0,0,120,80,this);
    }

    public static void main(String[] args) {
        Frame f = new Frame();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        f.add(new test(),BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
    }
}

or

import java.awt.*;
import java.applet.*;

public class testApp extends Applet {
    Image img;

    public void init() {
        img = getImage(getCodeBase(),"saturn.jpg");
    }

    public void paint(Graphics g) {
        g.drawImage(img,0,0,120,80,this);
    }
}

knute...
email s/nospam/knute/
crouching_buffy@hotmail.com - 24 Aug 2005 18:00 GMT
Thanks knute - plenty to think about there!
Ingo R. Homann - 15 Aug 2005 11:11 GMT
Hi,

> Hi
>
[quoted text clipped - 10 lines]
>             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
crouching_buffy@hotmail.com - 15 Aug 2005 18:51 GMT
OK, thanks everyone - I might just stick with the MediaTracker then, in
the meantime.

Ta v much
-Dave-
Knute Johnson - 15 Aug 2005 17:28 GMT
> Hi
>
[quoted text clipped - 5 lines]
> Ta
> -Dave-

Yes.  It works just fine.

knute...
email s/nospam/knute/
Roedy Green - 16 Aug 2005 02:39 GMT
>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
[quoted text clipped - 8 lines]
>            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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.