Here you can find the source of getImage(String url)
Parameter | Description |
---|---|
url | the image to fetch |
public static synchronized ImageIcon getImage(String url)
//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; } } }