package textbender.o.swing; // Copyright 2003-2006, 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.net.URL; import javax.swing.ImageIcon; import textbender.o.awt.ImageX; import textbender.g.util.logging.LoggerX; /** Image icon utilities. *

* Thread safe. *

*/ public class ImageIconX { private ImageIconX() {} /** Tries to create an image icon from a file. * * @param filename of image * @param size square size expected for the image * * @return image icon created from the file; * or the {@link ImageX#defaultImage() defaultImage} scaled to the expected size */ public static ImageIcon tryCreate( String filename, int size ) { return tryCreate( filename, size, size ); } /** Tries to create an image icon from a file. * * @param filename of image * @param width expected for the image * @param height expected for the image * * @return image icon created from the file; * or the {@link ImageX#defaultImage() defaultImage} scaled to the expected size */ public static ImageIcon tryCreate( String filename, int width, int height ) { // final ImageIcon imageIcon = new ImageIcon // ( Toolkit.getDefaultToolkit().getImage( filename )); // if( imageIcon.getIconWidth() != size || imageIcon.getIconHeight() != size ) // { // imageIcon.setImage // ( ImageX.defaultImage().getScaledInstance( size, size, Image.SCALE_FAST )); // } Image image = Toolkit.getDefaultToolkit().getImage( filename ); final boolean isLoadedFromSource; if( image == null ) { image = ImageX.defaultImage().getScaledInstance( width, height, Image.SCALE_FAST ); isLoadedFromSource = false; } else isLoadedFromSource = true; final ImageIcon imageIcon = new ImageIcon( image ); if( isLoadedFromSource ) checkSize( filename, width, height, imageIcon ); return imageIcon; } /** Tries to create an image icon from a URL. * * @param url of image, or null * @param size square size expected for the image * * @return image icon created from the URL; * or the {@link ImageX#defaultImage() defaultImage} scaled to the expected size */ public static ImageIcon tryCreate( URL url, int size ) { return tryCreate( url, size, size ); } /** Tries to create an image icon from a URL. * * @param url of image, or null * @param width expected for the image * @param height expected for the image * * @return image icon created from the URL; * or the {@link ImageX#defaultImage() defaultImage} scaled to the expected size */ public static ImageIcon tryCreate( URL url, int width, int height ) { // final ImageIcon imageIcon = new ImageIcon(); // if( url != null ) imageIcon.setImage( Toolkit.getDefaultToolkit().getImage( url )); // if( imageIcon.getIconWidth() != size || imageIcon.getIconHeight() != size ) // { // imageIcon.setImage // ( ImageX.defaultImage().getScaledInstance( size, size, Image.SCALE_FAST )); // } Image image = null; // till proven otherwise if( url != null ) image = Toolkit.getDefaultToolkit().getImage( url ); final boolean isLoadedFromSource; if( image == null ) { image = ImageX.defaultImage().getScaledInstance( width, height, Image.SCALE_FAST ); isLoadedFromSource = false; } else isLoadedFromSource = true; final ImageIcon imageIcon = new ImageIcon( image ); if( isLoadedFromSource ) checkSize( url.toString(), width, height, imageIcon ); return imageIcon; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static void checkSize( String sourceDescription, int widthExpected, int heightExpected, ImageIcon imageIcon ) { if( imageIcon.getIconWidth() != widthExpected ) LoggerX.i(ImageIconX.class).warning( "expected image width " + widthExpected + " does not match actual (" + imageIcon.getIconWidth() + "): " + sourceDescription ); if( imageIcon.getIconHeight() != heightExpected ) LoggerX.i(ImageIconX.class).warning( "expected image height " + heightExpected + " does not match actual (" + imageIcon.getIconHeight() + "): " + sourceDescription ); } }