Java BufferedImage Load loadImage(String imageName)

Here you can find the source of loadImage(String imageName)

Description

Loads an image from the given path.

License

LGPL

Parameter

Parameter Description
imageName path to image

Exception

Parameter Description
IllegalArgumentException when image can't be found in classpath

Return

loaded image

Declaration

public static Image loadImage(String imageName) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Image;

import java.awt.Toolkit;

import java.net.URL;

public class Main {
    private static final String IMAGE_PATH = "images/";

    /**//from  w w w . ja  v  a2  s . com
     * 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);
    }
}

Related

  1. loadImage(String fileName, Component c)
  2. loadImage(String fileName, Component component)
  3. loadImage(String filePath)
  4. loadImage(String i, int x, int imageWidth, int imageHeight)
  5. loadImage(String image)
  6. loadImage(String imageName, Component c)
  7. loadImage(String path)
  8. loadImage(String path)
  9. loadImage(String resourceName)