Loading images
8 July 2008You 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();
Related Posts:
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