View RSS Feed

My Java Tips

Loading images

Rate this Entry
by , 11-02-2011 at 06:05 PM (722 Views)
You can load GIF, JPEG, and (in SDK 1.3) PNG images using Toolkit's getImage() method:


Java Code:
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:


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

Submit "Loading images" to Facebook Submit "Loading images" to Digg Submit "Loading images" to del.icio.us Submit "Loading images" to StumbleUpon Submit "Loading images" to Google

Tags: None Add / Edit Tags
Categories
Java SE

Comments