Java Image Load getImage(String url)

Here you can find the source of getImage(String url)

Description

Returns an image depending on the URL.

License

LGPL

Parameter

Parameter Description
url the image to fetch

Return

the image found

Declaration

public static synchronized ImageIcon getImage(String url) 

Method Source Code

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

import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.ImageIcon;

public class Main {
    /** Caches images so they don't need to be loaded every time */
    private static Map<String, ImageIcon> imgCache = new ConcurrentHashMap<String, ImageIcon>();

    /**/*  w  w  w  .ja  v  a 2 s.c  o  m*/
     * Returns an image depending on the URL.
     * 
     * @param url
     *          the image to fetch
     * @return the image found
     */
    public static synchronized ImageIcon getImage(String 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);
            return null;
        }
    }
}

Related

  1. getImage(String imageName, String path)
  2. getImage(String path)
  3. getImage(String path)
  4. getImage(String text, boolean clockwise)
  5. getImage(URL url)
  6. getImageFromClassResource(Class cls, String resource)
  7. getImageFromFile(String fileName)
  8. getImageFromSource(JPanel source, Dimension dim)