Loading images

8 July 2008

You can load GIF, JPEG, and (in SDK 1.3) PNG images using Toolkit’s getImage() method:

Image i = Toolkit.getDefaultToolkit().getImage("car.png");

However, Images use lazy data loading, so the data for the image won’t start to be loaded until you try to display the image. You can use a MediaTracker to force the data to load, but it’s a pain in the butt. An easier solution is to use one of ImageIcon’s constructors, which does the dirty work of waiting for image data to load. Then you can just pull out the image and use it, confident in the knowledge that its data is fully loaded:

Image i = new javax.swing.ImageIcon("car.png").getImage();

del.icio.us:Loading images  digg:Loading images  spurl:Loading images  wists:Loading images  simpy:Loading images  newsvine:Loading images  blinklist:Loading images  furl:Loading images  reddit:Loading images  fark:Loading images  blogmarks:Loading images  Y!:Loading images  smarking:Loading images  magnolia:Loading images  segnalo:Loading images  gifttagging:Loading images

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Loading images

Leave a Reply