ImageUtil.java Source code

Java tutorial

Introduction

Here is the source code for ImageUtil.java

Source

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.net.URL;

public class ImageUtil {

    /**
     * Returns an image resource.
     *
     * @param filename the filename of the image to load
     * @return the loaded image
     */
    public static Image getImage(String filename) {
        URL url = ImageUtil.class.getResource(filename);

        if (url == null) {
            return null;
        }

        try {
            final BufferedImage result = ImageIO.read(url);

            if (result == null) {
                final String message = "Could not load image: " + filename;
                throw new Error();
            }
            return result;
        } catch (IOException e) {
            return null;
        }

    }

}