Package net.sourceforge.jiu.gui.awt
Class ToolkitLoader
- java.lang.Object
-
- net.sourceforge.jiu.gui.awt.ToolkitLoader
-
public class ToolkitLoader extends Object
This class loads an instance ofImage
usingToolkit
's built-in loading capabilities and converts it toRGB24Image
usingImageCreator
.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 ownToolkit
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'sAutoDetectColorType
withrgbImage
(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 callSystem.exit(0);
.- Author:
- Marco Schmidt
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ToolkitLoader()
This class has only static methods and fields, so there is no need to instantiate it.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 aRGB24Image
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 Detail
-
frame
private static Frame frame
-
-
Method Detail
-
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 AWTImage
object. This method does nothing more than callToolkit.getImage(String)
, wait for it using aMediaTracker
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 aRGB24Image
object. First callsload(java.lang.String)
with the filename, then converts the loaded image usingImageCreator.convertImageToRGB24Image(java.awt.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 callsloadViaToolkitOrCodecs(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). UsesloadAsRgb24Image(java.lang.String)
from this class for Toolkit loading andImageLoader
for JIU's codecs.- Parameters:
fileName
- name of the image file- Returns:
- image object or
null
on failure
-
-