Example usage for java.lang Class getResourceAsStream

List of usage examples for java.lang Class getResourceAsStream

Introduction

In this page you can find the example usage for java.lang Class getResourceAsStream.

Prototype

@CallerSensitive
public InputStream getResourceAsStream(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:PrintKTableExample.java

public Image loadImageResource(Display d, String name) {
    try {/*from   www  . j av a2  s  .c om*/

        Image ret = null;
        Class clazz = this.getClass();
        InputStream is = clazz.getResourceAsStream(name);
        if (is != null) {
            ret = new Image(d, is);
            is.close();
        }
        return ret;
    } catch (Exception e1) {
        return null;
    }
}

From source file:PrintKTableExample.java

protected Point calcSize() {
    try {//from  w ww. j a  v  a  2s . com
        Class clazz = new Object().getClass();
        InputStream is = clazz.getResourceAsStream(imgName);
        image = new Image(device, is);

        imgOriginalSize = new Point(0, 0);
        imgOriginalSize.x = image.getImageData().width;
        imgOriginalSize.y = image.getImageData().height;

        imgTargetSize = new Point(0, 0);
        imgTargetSize.x = (int) (imgOriginalSize.x * scalingPercent / 100 * (pixelPerInch.x / (double) imgDPI));
        imgTargetSize.y = (int) (imgOriginalSize.y * scalingPercent / 100 * (pixelPerInch.y / (double) imgDPI));

        sizeCalculatedfor = scalingPercent;

        image.getImageData().transparentPixel = -1;
        image.getImageData().maskData = null;

        return imgTargetSize;

    } catch (Exception e1) {
        System.out.println("could not open ressource " + imgName);
        imgOriginalSize = new Point(10, 10);
        imgTargetSize = new Point(10, 10);
        return imgTargetSize;
    }

}

From source file:CustomControlExample.java

/**
 * Loads the resources/* w w w.ja v a 2s.  c o m*/
 */
void initResources() {
    final Class clazz = ControlExample.class;
    try {
        if (images == null) {
            images = new Image[imageLocations.length];

            for (int i = 0; i < imageLocations.length; ++i) {
                InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i]);
                ImageData source = new ImageData(sourceStream);
                ImageData mask = source.getTransparencyMask();
                images[i] = new Image(null, source, mask);
                try {
                    sourceStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return;
    } catch (Throwable t) {
    }
    String error = "Unable to load resources";
    freeResources();
    throw new RuntimeException(error);
}