Thursday, July 12, 2012

How to use Fully Load Images Using ImageIcon?

You can load GIF, JPEG, and (in SDK 1.3) PNG images using Toolkit's getImage() method:
Image i = Toolkit.getDefaultToolkit().getImage("tarsier.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("tarsier.png").getImage();

No comments:

Post a Comment