Here you can find the source of loadImageIcon(String imageName)
Parameter | Description |
---|---|
imageName | path to image |
Parameter | Description |
---|---|
IllegalArgumentException | when image can't be found in classpath |
public static ImageIcon loadImageIcon(String imageName)
//package com.java2s; //License from project: LGPL import java.awt.Image; import java.awt.Toolkit; import java.net.URL; import javax.swing.ImageIcon; public class Main { private static final String IMAGE_PATH = "images/"; /**//from w w w .j a va 2s .c om * Loads an image from a given path and returns it as an ImageIcon. * * @param imageName path to image * @return loaded image * @throws IllegalArgumentException when image can't be found in classpath */ public static ImageIcon loadImageIcon(String imageName) { return new ImageIcon(loadImage(imageName)); } /** * Loads an image from the given path. * * @param imageName path to image * @return loaded image * @throws IllegalArgumentException when image can't be found in classpath */ public static Image loadImage(String imageName) { URL imageUrl = Thread.currentThread().getContextClassLoader().getResource(IMAGE_PATH + imageName); if (imageUrl == null) { throw new IllegalArgumentException("'" + imageName + "' could not be found in classpath"); } return Toolkit.getDefaultToolkit().getImage(imageUrl); } }