Here you can find the source of getScaledIcon(String resourceName, int width, int height)
Parameter | Description |
---|---|
resourceName | Name of resource where icon is stored. |
width | Width for the returned icon. |
height | Height for the returned icon. |
null
if resource is not available.
public static ImageIcon getScaledIcon(String resourceName, int width, int height)
//package com.java2s; /*// w w w . j a v a 2 s. c om * Swing Explorer. Tool for developers exploring Java/Swing-based application internals. * Copyright (C) 2012, Maxim Zakharenkov * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import java.awt.Image; import java.awt.Toolkit; import java.net.URL; import javax.swing.ImageIcon; public class Main { /** * Used to get an icon with specified width and height. If the width or * height are less or equal with 0 icon will not be scaled. * @param resourceName Name of resource where icon is stored. * @param width Width for the returned icon. * @param height Height for the returned icon. * @return The created and scaled icon or <code>null</code> if resource * is not available. */ public static ImageIcon getScaledIcon(String resourceName, int width, int height) { ImageIcon result = getImageIcon(resourceName); if (result != null) { if (width > 0 && height > 0) { Image artImage = result.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH); result.setImage(artImage); } } return result; } /** * Used to get instance of <i>ImageIcon</i> object. Invoke * {@link #getImageIcon(Class, String)} method with <code>null</code> class. * @param resourceName Name of the resource where image stored. * @return Instance of <i>ImageIcon</i> object or <code>null</code> if * resource is not found. */ public static ImageIcon getImageIcon(String resourceName) { return getImageIcon(null, resourceName); } /** * Used to get instance of <i>ImageIcon</i> object. Specified class or * <i>ClassLoader</i> if class is null are used to get access to the * resource where image is stored. * @param _class Class used to get the image resource. * May be <code>null</code>. * @param resourceName Name of the resource where image is stored. * @return Instance of <i>Image</i> object or <code>null</code> if * resource is not found. */ public static ImageIcon getImageIcon(Class<?> _class, String resourceName) { Image image = getImage(_class, resourceName); if (image != null) { return new ImageIcon(image); } else { return null; } } /** * Used to get instance of <i>Image</i> object using <i>ClassLoader</i> as * resource provider. * @param resourceName Name of the resource where image stored. * @return Instance of <i>Image</i> object or <code>null</code> if * resource is not found. */ public static Image getImage(String resourceName) { return getImage(null, resourceName); } /** * Used to get instance of <i>Image</i> object. Specified class or * <i>ClassLoader</i> if class is null are used to get access to the * resource where image is stored. * @param _class Class used to get the image resource. * May be <code>null</code>. * @param resourceName Name of the resource where image is stored. * @return Instance of <i>Image</i> object or <code>null</code> if * resource is not found. */ public static Image getImage(Class<?> _class, String resourceName) { URL resource; if (_class != null) { // Strange things happens here: // The following code must work in web start, // but is not working in local Java application. resource = _class.getClassLoader().getResource(resourceName); // that's why following code used: if (resource == null) { resource = _class.getResource(resourceName); } } else { resource = Thread.currentThread().getContextClassLoader().getResource(resourceName); } if (resource != null) { return Toolkit.getDefaultToolkit().getImage(resource); } else { return null; } } }