ImageLoader.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.paint.helpers;

import java.io.InputStream;
import javax.microedition.lcdui.Image;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableImage;

// ImageLoader version 2.1
public class ImageLoader {

    private static ImageLoader self;

    private ImageLoader() {
    }

    public static ImageLoader getInstance() {
        if (self == null) {
            self = new ImageLoader();
        }
        return self;
    }

    public Image loadImage(String imagepath) throws RuntimeException {
        Image image;
        try {
            InputStream in = getClass().getResourceAsStream(imagepath);
            image = Image.createImage(in);
        }
        catch (Exception e) {
            throw new RuntimeException("ImageLoader failed to load image:" + imagepath + " " + e.getMessage());
        }
        return image;
    }

    public SVGImage loadSVGImage(String imagepath) throws RuntimeException {
        SVGImage svg_image;
        try {
            InputStream in = getClass().getResourceAsStream(imagepath);
            svg_image = (SVGImage) ScalableImage.createImage(in, null);
        }
        catch (Exception io) {
            throw new RuntimeException("ImageLoader failed to load SVG image:" + imagepath + " " + io.getMessage());
        }
        return svg_image;
    }
}