Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

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?
0
wgilster
Asked:
wgilster
  • 7
  • 6
1 Solution
 
CEHJCommented:
0
 
wgilsterAuthor Commented:
Great for 1.4 but what about 1.3?   No ImageIO class
0
 
CEHJCommented:
>>but what about 1.3?

You can make things easier by doing

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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

0
 
wgilsterAuthor Commented:
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...
0
 
wgilsterAuthor Commented:
Sorry, My bad, ByteArrayOutputStream is there.

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

It has been in the API since Java 1.0
0
 
CEHJCommented:
>>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
0
 
wgilsterAuthor Commented:
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.
0
 
CEHJCommented:
Why not just use the ImageIcon as i mentioned?
0
 
wgilsterAuthor Commented:
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...
0
 
CEHJCommented:
8-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now