Java ImageIcon Load getImageIcon(Class _class, String resourceName)

Here you can find the source of getImageIcon(Class _class, String resourceName)

Description

Used to get instance of ImageIcon object.

License

Open Source License

Parameter

Parameter Description
_class Class used to get the image resource. May be <code>null</code>.
resourceName Name of the resource where image is stored.

Return

Instance of Image object or null if resource is not found.

Declaration

public static ImageIcon getImageIcon(Class<?> _class, String resourceName) 

Method Source Code

//package com.java2s;
/*//from  ww  w. j  a v a2s. co  m
 *   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 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;
        }
    }
}

Related

  1. getImageIcon(final Class baseClass, final String image)
  2. getImageIcon(final String iconName)
  3. getImageIcon(final String location)
  4. getImageIcon(Image imp, int width, int height)