Class ToolkitLoader


  • public class ToolkitLoader
    extends Object
    This class loads an instance of Image using 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 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 Detail

      • frame

        private static Frame frame
    • Constructor Detail

      • 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 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 AWT Image object. This method does nothing more than call Toolkit.getImage(String), wait for it using a MediaTracker and return the resulting image.
        Parameters:
        fileName - name of the image file
        Returns:
        the image as AWT image object
      • 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(java.lang.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