Here you can find the source of loadImage(final String imagePathname, final Class relatedClass)
Parameter | Description |
---|---|
imagePathname | A pathname relative to the directory holding the class file of the given relatedClass. For example, <code>images/MyIcon.gif</code>. |
relatedClass | the class the image is related with. |
public static final java.awt.Image loadImage(final String imagePathname, final Class relatedClass)
//package com.java2s; public class Main { /**/*from www. j a v a2 s . c o m*/ * This is a utility method to help in loading icon images. It takes the pathname of a resource file * associated with the a given class and loads an image object from that file. Typically images will be GIFs. * <p> * The pathname should be relative to the given class and be contained in the classpath. * For instance if the related class is in <code>cern.gp.beans</code> and the icon is stored in * <code>cern/gp/beans/images/MyIcon.gif</code> the pathname to give would be * <code>images/MyIcon.gif</code>. * </p> * @param imagePathname A pathname relative to the directory holding the class file of * the given relatedClass. For example, <code>images/MyIcon.gif</code>. * @param relatedClass the class the image is related with. * @return an image object. May be null if the load failed. */ public static final java.awt.Image loadImage(final String imagePathname, final Class relatedClass) { try { java.awt.image.ImageProducer ip = (java.awt.image.ImageProducer) java.security.AccessController .doPrivileged(new java.security.PrivilegedAction() { public Object run() { java.net.URL url = relatedClass.getResource(imagePathname); if (url == null) return null; try { return url.getContent(); } catch (java.io.IOException ioe) { return null; } } }); if (ip == null) return null; java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit(); return tk.createImage(ip); } catch (Exception ex) { return null; } } }