Java Utililty Methods Image Load

List of utility methods to do Image Load

Description

The list of methods to do Image Load are organized into topic(s).

Method

ImagegetImage(String imageName, String path)
get Image
return new ImageIcon(getImageURL(imageName, path)).getImage();
ImagegetImage(String path)
get Image
URL url = System.class.getResource(path);
if (url == null) {
    System.err.println("Unable to load image: " + path);
ImageIcon icon = new ImageIcon(url);
return icon.getImage();
ImagegetImage(String path)
get Image
URL url = System.class.getResource(path);
if (url == null) {
    System.err.println("Unable to load image: " + path);
return new ImageIcon(url).getImage();
BufferedImagegetImage(String text, boolean clockwise)
get Image
JLabel lbl = new JLabel();
Font f = lbl.getFont();
f = f.deriveFont(Font.BOLD);
FontMetrics fm = lbl.getFontMetrics(f);
Color bg = new Color(1f, 1f, 1f, 0.0f);
Color fontColor = lbl.getForeground();
return getImage(text, clockwise, f, fm, bg, fontColor);
ImageIcongetImage(String url)
Returns an image depending on the URL.
if (imgCache.containsKey(url))
    return imgCache.get(url);
URL imgURL = Main.class.getResource("/res/img/" + url);
if (imgURL != null) {
    imgCache.put(url, new ImageIcon(imgURL));
    return imgCache.get(url);
} else {
    System.err.println("Couldn't find file: " + url);
...
ImagegetImage(URL url)
get Image
ImageIcon icon = getImageIcon(url);
return (icon != null) ? icon.getImage() : null;
ImagegetImageFromClassResource(Class cls, String resource)
Get an Image from a resource in a class.
if (cls == null || resource == null)
    return null;
java.net.URL imgURL = cls.getResource(resource);
if (imgURL != null) {
    return new ImageIcon(imgURL).getImage();
} else {
    return null;
ImageIcongetImageFromFile(String fileName)
get Image From File
try {
    final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    buffer.flush();
    return new ImageIcon(buffer.toByteArray());
} catch (IOException e) {
    e.printStackTrace();
return null;
BufferedImagegetImageFromSource(JPanel source, Dimension dim)
get Image From Source
int width = dim.width;
int height = dim.height;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
Rectangle originalBounds = source.getBounds();
source.setBounds(0, 0, width, height);
source.updateUI();
source.paint(g);
...
BufferedImagegetLocal(String imagePath)
get Local
try {
    return toBufferedImage(Toolkit.getDefaultToolkit().getImage(imagePath));
} catch (Throwable e) {
    return ImageIO.read(new File(imagePath));