load ImageIcon - Java 2D Graphics

Java examples for 2D Graphics:Image Load

Description

load ImageIcon

Demo Code

/*//from  w  w w  . j  a v  a  2 s  .c om
 * Copyright 2005 MH-Software-Entwicklung. All rights reserved.
 * Use is subject to license terms.
 */
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;

public class Main{
    public static ImageIcon loadImage(String name) {
        ImageIcon image = null;
        try {
            URL url = Main.class.getResource(name);
            if (url != null) {
                java.awt.Image img = Toolkit.getDefaultToolkit()
                        .createImage(url);
                if (img != null)
                    image = new ImageIcon(img);
            }
        } catch (Throwable ex) {
            System.out.println("ERROR: loading image " + name + " failed");
        }
        return image;
    }
}

Related Tutorials