get Image From Resource - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

get Image From Resource

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    public static BufferedImage getImageFromResource(String name) {

        BufferedImage img = null;

        String path = ClassLoader.getSystemResource("").getFile();

        path = path.substring(0, path.length() - 4);

        path += "res/img/" + name;

        try {/*from   ww w.j  a va2  s.c o  m*/
            img = ImageIO.read(new File(path));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return img;
    }
}

Related Tutorials