Java BufferedImage Load loadImage(final String imagePathname, final Class relatedClass)

Here you can find the source of loadImage(final String imagePathname, final Class relatedClass)

Description

This is a utility method to help in loading icon images.

License

Open Source License

Parameter

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.

Return

an image object. May be null if the load failed.

Declaration

public static final java.awt.Image loadImage(final String imagePathname, final Class relatedClass) 

Method Source Code

//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;
        }
    }
}

Related

  1. loadImage(final byte[] imageData)
  2. loadImage(final File imgFile)
  3. loadImage(final File refDirectory, final String imageFileName)
  4. loadImage(final String fileName)
  5. loadImage(final String iconFile)
  6. loadImage(final String path)
  7. loadImage(final URL urlToImage)
  8. loadImage(Image image)
  9. loadImage(InputStream inputStream)