Here you can find the source of loadImagefromJar(Object refObj, String fileName)
This method loads a picture from a relative path string.
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. |
public static BufferedImage loadImagefromJar(Object refObj, String fileName)
//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; } }