SplashScreen.java

/**
 * Copyright (c) 2013 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.statusshout;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * A Canvas based class for displaying the splash screen image.
 */
public class SplashScreen extends Canvas {
    // Members
    private Image splashScreenImage;
    private int backgroundColor;
    private int centerX;
    private int centerY;

    /**
     * Constructor.
     * 
     * @param imageUri The splash screen image URI.
     * @param backgroundColor The background color to fill the parts of the
     * screen that are not covered by the image.
     */
    public SplashScreen(final String imageUri, final int backgroundColor) {
        super();
        this.setFullScreenMode(true);
        this.backgroundColor = backgroundColor;
        
        try {
            splashScreenImage = Image.createImage(imageUri);
        }
        catch (IOException e) {
            System.out.println("Failed to load the splash screen image: " + e.toString());
        }
        
        if (splashScreenImage != null) {
            // Calculate the center position for the image
            centerX = (getWidth() - splashScreenImage.getWidth()) / 2;
            centerY = (getHeight() - splashScreenImage.getHeight()) / 2;
        }
    }

    /** 
     * @return The splash screen image.
     */
    public Image getImage() {
        return splashScreenImage;
    }

    /**
     * Paints the splash screen.
     * 
     * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
     */
    protected void paint(Graphics graphics) {
        graphics.setColor(backgroundColor);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        
        if (splashScreenImage != null) {
            graphics.drawImage(splashScreenImage, centerX, centerY,
                    Graphics.TOP | Graphics.LEFT);
        }
    }
}