Java Image Load getImageFromClassResource(Class cls, String resource)

Here you can find the source of getImageFromClassResource(Class cls, String resource)

Description

Get an Image from a resource in a class.

License

Open Source License

Parameter

Parameter Description
cls The class.
resource Path of the resource, i.e. "images/main.gif", relative to the class.

Declaration

public static Image getImageFromClassResource(Class<?> cls, String resource) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Image;

import javax.swing.ImageIcon;

public class Main {
    /**/*from w  w  w  . j  a va2s  . co m*/
     * Get an Image from a resource in a class.
     * 
     * @param cls The class.
     * @param resource Path of the resource, i.e. "images/main.gif", relative to
     *            the class.
     * @return
     */
    public static Image getImageFromClassResource(Class<?> cls, String resource) {
        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;
        }
    }
}

Related

  1. getImage(String path)
  2. getImage(String path)
  3. getImage(String text, boolean clockwise)
  4. getImage(String url)
  5. getImage(URL url)
  6. getImageFromFile(String fileName)
  7. getImageFromSource(JPanel source, Dimension dim)
  8. getLocal(String imagePath)
  9. loadImage(File original)