Returns an ImageIcon, or null if the path was invalid. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Returns an ImageIcon, or null if the path was invalid.

Demo Code


//package com.java2s;

import javax.swing.ImageIcon;

public class Main {
    /** Returns an ImageIcon, or null if the path was invalid. */
    @Deprecated// w  w w  .ja  va2  s  . c  om
    public ImageIcon createImageIcon(String path, String description) {
        System.err
                .println("createImageIcon(String path, String description) is deprecated");
        System.err.println("Use the static method instead");
        return null;
    }

    /** 
     * Returns an ImageIcon, or null if the path was invalid.
     * The Path is given from the class using this method.
     * 
     * Example : the image "img.png" is in the same package as obj.
     * <code>ImageIcon img = GraphicHelper.createImageIcon(obj, "img.png", "An example");
     * 
     * @param from the Object loading the ImageIcon
     * @param path Either absolute (begins with '/') or relative to object package.
     * @param description A string used as image description.
     * 
     * @return null (Image not Found) or ImageIcon
     */
    static public ImageIcon createImageIcon(Object from, String path,
            String description) {
        java.net.URL imgURL = from.getClass().getResource(path);
        if (imgURL != null) {
            ImageIcon img = new ImageIcon(imgURL, description);
            return img;
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

Related Tutorials