Java BufferedImage Load loadImagefromJar(Object refObj, String fileName)

Here you can find the source of loadImagefromJar(Object refObj, String fileName)

Description

This method loads a picture from a relative path string.

License

Open Source License

Parameter

Parameter Description
refObj - Reference Object(Object) - meant for a standard 'this' call, though any Instantiated class can be used. This is part of a workaround for Netbean's multiple class loader system.
fileName - File Name(String) - the path to an image relative to the Reference Object's location in a jar file.

Return

BufferedImage - Freshly converted from the image file found at the location.

Declaration

public static BufferedImage loadImagefromJar(Object refObj,
        String fileName) 

Method Source Code

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

import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /**//from   w w  w .  j a  v  a2 s  .  c  o  m
     * <p>
     * This method loads a picture from a relative path string. The relative
     * path's root directory is understood to be inside of a jar... and in
     * relation to the package of the referring Object instance.
     * </p>
     * <p>
     * For example: if the object is an instance of
     * org.jme3.netbeans.plaf.darkmonkey.DarkMonkeyIconFactory.class, and the
     * string is "icons/MyCloseIcon.png", it will attempt to load
     * org/jme3/netbeans/plaf/darkmonkey/icons/MyCloseIcon.png from
     * DarkMonkeyIconFactory's jar file.
     * </p>
     * It will print a stack trace if you get the relative path wrong.
     *
     * @param refObj - Reference Object(Object) - meant for a standard 'this'
     * call, though any Instantiated class can be used. This is part of a
     * workaround for Netbean's multiple class loader system.
     * @param fileName - File Name(String) - the path to an image relative to
     * the Reference Object's location in a jar file.
     * @return BufferedImage - Freshly converted from the image file found at
     * the location.
     */
    public static BufferedImage loadImagefromJar(Object refObj,
            String fileName) {
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(refObj.getClass().getResourceAsStream(
                    fileName));
        } catch (IOException e) {
            // File is probably referenced wrong or "mispleled"... lol.
            e.printStackTrace();
        }
        return bi;
    }
}

Related

  1. loadImageFromFile(File file)
  2. loadImageFromFile(String filename)
  3. loadImageFromFile(String fileName)
  4. loadImageFromFile(String filepath)
  5. loadImageFromFile_ORIG(String imgName)
  6. loadImageFromURL(String imageURL, Component component)
  7. loadImageFromVisualgorithmJar(String imageFileName)
  8. loadImageRessource(Object source, String file)
  9. loadImages(Image[] images, Component comp)