Loads an image from the resource directory. - Java 2D Graphics

Java examples for 2D Graphics:Image Load

Description

Loads an image from the resource directory.

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

import java.io.InputStream;

import javax.imageio.ImageIO;

public class Main {
    public static void main(String[] argv) throws Exception {
        String filename = "java2s.com";
        System.out.println(loadImage(filename));
    }//from   w w w .  ja v  a  2s  .  co  m

    private static String resources = "";
    private static ClassLoader loader;

    /**
     * Loads an image from the resource directory.
     * 
     * @param filename
     *        => The name of the file in the resource directory.
     */
    public static BufferedImage loadImage(String filename) throws Exception {
        InputStream stream = loader.getResource(resources + filename)
                .openStream();
        return ImageIO.read(stream);
    }
}

Related Tutorials