net.sourceforge.jiu.gui.awt

Class ToolkitLoader


public class ToolkitLoader
extends java.lang.Object

This class loads an instance of java.awt.Image using java.awt.Toolkit's built-in loading capabilities and converts it to RGB24Image using ImageCreator.

Supported file formats are JPEG and GIF. PNG is supported since Java 1.3. I have heard that XBM are supposedly loaded as well. I don't know that format and haven't tested this functionality.

In addition, this class can also use JIU's built-in codecs from this class.

Usage examples

Load an image using Java's own java.awt.Toolkit class:
 RGB24Image rgbImage = ToolkitLoader.loadAsRgb24Image("flower.jpg");
 
This will only load images from files in formats that are supported by Toolkit - normally, that only includes JPEG, GIF and since Java 1.3 PNG. A potential problem of this approach is that Toolkit always delivers RGB data, even if the image file only contains a black and white image. In order to get an image object of the "real" type, try JIU's AutoDetectColorType with rgbImage (if you follow the link you will get a usage example for that class as well).

Known issues

If you are using this class to load JPEGs, GIFs or PNGs, an AWT background thread is started (as for a normal AWT GUI application). Before Java 1.4 there was a bug that kept the thread running although an application had reached the end of its execution (by getting to the end of the main(String[]) method). If you experience this problem, either update to a 1.4+ JDK or follow the advice given at jguru.com and call System.exit(0);.
Author:
Marco Schmidt

Field Summary

private static Frame
frame

Constructor Summary

ToolkitLoader()
This class has only static methods and fields, so there is no need to instantiate it.

Method Summary

static Image
load(String fileName)
Loads an image from a file using the AWT's built-in loader.
static RGB24Image
loadAsRgb24Image(String fileName)
Loads an image from a file using the AWT's built-in loader and converts the image to a RGB24Image object.
static PixelImage
loadViaToolkitOrCodecs(String fileName)
Attempts to load an image from a file given by its name, using both the JIU codecs and the image loading functionality in java.awt.Toolkit.
static PixelImage
loadViaToolkitOrCodecs(String fileName, boolean preferToolkit, Vector progressListeners)
Attempts to load an image from a file given by its name, using both the JIU codecs and the image loading functionality in java.awt.Toolkit.

Field Details

frame

private static Frame frame

Constructor Details

ToolkitLoader

private ToolkitLoader()
This class has only static methods and fields, so there is no need to instantiate it. That's why the empty constructor is hidden here.

Method Details

load

public static Image load(String fileName)
Loads an image from a file using the AWT's built-in loader. Returns that image as an AWT java.awt.Image object. This method does nothing more than call java.awt.Toolkit.getImage(String), wait for it using a java.awt.MediaTracker and return the resulting image.
Parameters:
fileName - name of the image file
Returns:
the image as AWT image object

loadAsRgb24Image

public static RGB24Image loadAsRgb24Image(String fileName)
Loads an image from a file using the AWT's built-in loader and converts the image to a RGB24Image object. First calls load(String) with the filename, then converts the loaded image using ImageCreator.convertImageToRGB24Image(Image).
Parameters:
fileName - name of the file from which the image is to be loaded
Returns:
loaded image as RGB24Image

loadViaToolkitOrCodecs

public static PixelImage loadViaToolkitOrCodecs(String fileName)
Attempts to load an image from a file given by its name, using both the JIU codecs and the image loading functionality in java.awt.Toolkit. First tries JIU's codecs, then java.awt.Toolkit. Simply calls loadViaToolkitOrCodecs(fileName, false);.
Parameters:
fileName - name of the image file
Returns:
image object or null on failure

loadViaToolkitOrCodecs

public static PixelImage loadViaToolkitOrCodecs(String fileName,
                                                boolean preferToolkit,
                                                Vector progressListeners)
Attempts to load an image from a file given by its name, using both the JIU codecs and the image loading functionality in java.awt.Toolkit. The second argument determines which method is tried first, Toolkit (true) or the JIU codecs (false). Uses loadAsRgb24Image(String) from this class for Toolkit loading and ImageLoader for JIU's codecs.
Parameters:
fileName - name of the image file
Returns:
image object or null on failure