Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public final class Main {

    private Main() {
    }

    public static Image getImage(Class relativeClass, String filename) {
        Image returnValue = null;
        InputStream is = relativeClass.getResourceAsStream(filename);
        if (is != null) {
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                int ch;
                while ((ch = bis.read()) != -1) {
                    baos.write(ch);
                }
                Toolkit t = Toolkit.getDefaultToolkit();
                returnValue = t.createImage(baos.toByteArray());
            } catch (IOException exception) {
                System.err.println("Error loading: " + filename);
            }
        }
        return returnValue;
    }
}