SplashScreen.java

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

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * The splash screen. 
 */
public class SplashScreen extends Canvas {
    // Members
    private final Listener _listener;
    private Image _image;
    private final int _backgroundColor;
    private int _centerX;
    private int _centerY;
    
    /**
     * Creates a splash screen to display the specified image and fills the
     * background with the specified color.
     * 
     * @param backgroundColor The background color.
     */
    public SplashScreen(Listener listener, final int backgroundColor) {
        setFullScreenMode(true);
        _listener = listener;
        _backgroundColor = backgroundColor;
    }

    /**
     * From Canvas.
     */
    protected void paint(Graphics graphics) {
        graphics.setColor(_backgroundColor);
        graphics.fillRect(0x00, 0x00, getWidth(), getHeight());

        if (_image != null) {
            graphics.drawImage(_image, _centerX, _centerY, 0x00);
        }
    }

    /**
     * Loads the splash screen image.
     * @param image The path of the image to load.
     */
    public void loadImage(String image) {
        try {
            _image = Image.createImage(image);
            
            // Calculate the center position
            _centerX = (getWidth() - _image.getWidth()) / 2;
            _centerY = (getHeight() - _image.getHeight()) / 2;
        }
        catch (IOException e) {}
    }
    
    /**
     * Shows the splash and changes to the specified displayable after the
     * specified number of milliseconds.
     * 
     * @param display Current Display.
     * @param next new Displayable.
     * @param millis splash time in milliseconds.
     */
    public void show(final Display display,
                     final Displayable next,
                     final long millis)
    {
        display.setCurrent(this);

        // Schedule next Displayable
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(millis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    display.setCurrent(next);
                    
                    if (_listener != null) {
                        _listener.onFinished();
                    }
                }
            }
        });
        t.start();
    }
    
    /**
     * Interface for MIDlet so that it can know when the splash screen can be
     * released for garbage collection.
     */
    public interface Listener {
        void onFinished();
    }
}