Link to home
Start Free TrialLog in
Avatar of wgilster
wgilster

asked on

MediaTracker, but no Component

I need to load images using an application but I don't have any components to construct a mediatracker.  What is most commonly done in this case?  Is there a component somewhere in the toolkit for this, or do I just create my own.  I realize I can do this:
new MediaTracker(new JLabel());
But why isn't their a default constructor for such a common case?  Am I missing something?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

No need for MediaTracker - see next question down:

http://oldlook.experts-exchange.com/questions/21202103/Crop-jpg-image.html
Avatar of wgilster
wgilster

ASKER

Great for 1.4 but what about 1.3?   No ImageIO class
>>but what about 1.3?

You can make things easier by doing

Image image = new ImageIcon("x.jpg").getImage();

I have a stream of bytes, not a filename that will be turned into an image.
Use a ByteArrayOutputStream (let it be 'baos') and then do

Image image = new ImageIcon(baos.toByteArray()).getImage();

Great for 1.4 but what about 1.3?  No ByteArrayOutputStream class.  

Do I have to read it into an array?  If so, let's get back to the mediatracker...
Sorry, My bad, ByteArrayOutputStream is there.

Do I have to write my inputstream to the outputstream?
>>No ByteArrayOutputStream class.  

It has been in the API since Java 1.0
>>Do I have to write my inputstream to the outputstream?

You read the bytes from your 'stream of bytes' then write them to the baos
I figured it out, ImageIcon uses a mediatracker with an anonymous class like this...

    protected final static Component component = new Component() {};
    protected final static MediaTracker tracker = new MediaTracker(component);

That's what I'll do.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My entire class was based around the MediaTracker with a null parameter, but I kept getting NullPointerExceptions.  I won't have to change my program at all this way.  But you led me to the answer...
8-)