package textbender.o.awt; // Copyright 2001-2005, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.*; import java.awt.image.MemoryImageSource; import java.io.IOException; import java.net.URL; /** Image utilities. */ public class ImageX { private ImageX() {} /** A default image to use when the desired image is unavailable. * It is a 2x2 pattern of coloured squares, that easily scales to any size. */ public static Image defaultImage() { return defaultImage; } private static final Image defaultImage = Toolkit.getDefaultToolkit().createImage ( new java.awt.image.MemoryImageSource ( 2,2, // width, height new int[] { Color.black.getRGB(), Color.yellow.getRGB(), Color.white.getRGB(), Color.red.getRGB() }, 0,2 // offset, scan (row length) ) ); /** A null implementation of an image, having dimensions of zero. */ public static Image i0() { return i0; } private static final Image i0 = Toolkit.getDefaultToolkit().createImage ( new MemoryImageSource ( /*width*/0, /*height*/0, /*pixels*/new int[] {}, /*offset*/0, /*scan*/0 ) ); // /** Tries to create an image icon from a file. // * // * @param filename of image // * @param size expected for the image // * // * @return image created from the file; // * or the {@link #defaultImage() defaultImage} scaled to the expected size // */ // public static Image tryCreate( String filename, int size ) // { // Image image = Toolkit.getDefaultToolkit().getImage( filename ); // if( image != null ) return image; // else return defaultImage.getScaledInstance( size, size, Image.SCALE_FAST ); // } ///////// but if the file does not exist, getImage() returns an empty image, which is hard to detect, so use com.zelea.util.java.swing.FailSafeImageIcon /** Tries to create an image from a URL. * * @param url of the image * @param size expected for the image * * @return image created from the URL; * or the {@link #defaultImage() defaultImage} scaled to the expected size */ public static Image tryCreate( URL url, int size ) // never tested { Image image = null; // till proven otherwise try { url.openConnection().connect(); // OPT, merely assuming that these connections close themselves, as there is no explicit way to do it // No IOException? Then the image exists. // Otherwise, without this test, it's hard to know; // because getImage() below may return an empty image // and finish loading it later, if it really exists. image = Toolkit.getDefaultToolkit().getImage( url ); } catch( IOException x ) {} if( image != null ) return image; else return defaultImage.getScaledInstance( size, size, Image.SCALE_FAST ); } }