Java Utililty Methods ImageIcon Create

List of utility methods to do ImageIcon Create

Description

The list of methods to do ImageIcon Create are organized into topic(s).

Method

ImageIconcreateImageIcon(Class clazz, String path)
Returns an ImageIcon, or throws a RuntimeException if the path was invalid.
URL imageUrl = clazz.getResource(path);
if (imageUrl != null) {
    return new ImageIcon(imageUrl);
throw new RuntimeException("Couldn't find file: " + path);
ImageIconcreateImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY)
create Image Icon
ImageIcon ret = null;
if (pathToImage != null) {
    try {
        InputStream is = clazz.getResourceAsStream(pathToImage);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = is.read(buffer)) != -1) {
...
ImageIconcreateImageIcon(Class theClass, String path, String description)
Loads the image icon from a package.
URL imgURL = theClass.getClass().getResource(path);
if (imgURL != null) {
    return new ImageIcon(imgURL, description);
} else {
    return null;
ImageIconcreateImageIcon(Class bezugsklasse, String path)
create Image Icon
final URL imgURL = bezugsklasse.getResource(path);
if (imgURL != null) {
    return new ImageIcon(imgURL);
System.err.println("Couldn't find file: " + path);
return null;
ImageIconcreateImageIcon(ClassLoader classloader, String path, String description)
create Image Icon
URL url = classloader.getResource(path);
if (null != url) {
    return new ImageIcon(url);
} else {
    final String error = "Supplied image icon resource path is unknown [" + path + "].";
    throw new IllegalArgumentException(error);
ImageIconcreateImageIcon(File f, String description)
Load BufferedImage form file, scale it and converts to icon.
try {
    BufferedImage i = ImageIO.read(f);
    return new ImageIcon(i.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
} catch (Exception ex) {
    return null;
ImageIconcreateImageIcon(final Icon icon)
Creates an ImageIcon from any Icon.
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
        BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
icon.paintIcon(new JLabel(), g, 0, 0);
g.dispose();
return new ImageIcon(bufferedImage);
ImageIconcreateImageIcon(final String src)
create Image Icon
if (!new File(src).exists()) {
    return new ImageIcon();
} else {
    try {
        return new ImageIcon(ImageIO.read(new File(src)));
    } catch (final Throwable e) {
        return new ImageIcon();
ImageIconcreateImageIcon(java.net.URL imgURL)
create Image Icon
return new ImageIcon(imgURL);
ImageIconcreateImageIcon(String iconFile)
Returns an ImageIcon, or null if the iconFile is invalid.
if (iconFile != null) {
    return new ImageIcon(iconFile);
} else {
    System.err.println("Couldn't find file: " + iconFile);
    return null;