<< Java ME SDK 3.0 and IntelliJ IDEA | Home | Sony Ericsson emulator problem on 64bit Windows >>

Simple splash screen

Sample of splash screen

I had this peace of code in my collection for sometime and forgot to put it online. This is simple splash screen midlet that is displayed on application start-up for 3 seconds and then next screen is showed.

The whole idea is very simple:

  1. start MIDlet
  2. redirect it to next screen that is Canvas
  3. in Canvas display image
  4. create and start new Thread with pre-set sleep time (in this scenario 3 seconds)
  5. when time is out forward action to next screen

In addition to this splash screen can be dismissed by key release or on touch type device by pointer (touch) release.

package splashdemo;

/**
 * Created by IntelliJ IDEA.
 * User: Peter Miklosko
 * URL: http://www.peterscorner.co.uk
 * Date: 01-Nov-2009
 * Time: 13:59:43
 */
import java.io.IOException;

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

import splashdemo.SplashMIDlet;

/**
 * A simple splash screen.
 */
public class SplashScreen extends Canvas implements Runnable {

    private Image mImage;
    private SplashMIDlet midlet;

    /**
     * The constructor attempts to load the named image and begins a timeout
     * thread. The splash screen can be dismissed with a key press,
     * a pointer press, or a timeout
     * @param projectMIDlet instance of MIDlet
     */
    public SplashScreen(SplashMIDlet projectMIDlet){
        this.midlet = projectMIDlet;
        try{
        mImage = Image.createImage("/res/sourcesense.png");
        Thread t = new Thread(this);
        t.start();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    /**
     * Paints the image centered on the screen.
     */
    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        //set background color to overdraw what ever was previously displayed
        g.setColor(0x000000);
        g.fillRect(0,0, width, height);
        g.drawImage(mImage, width / 2, height / 2,
                Graphics.HCENTER | Graphics.VCENTER);
    }

    /**
     * Dismisses the splash screen with a key press or a pointer press
     */
    public void dismiss() {
        if (isShown())
            Display.getDisplay(midlet).setCurrent(new NextScreen(midlet));
    }

    /**
     * Default timeout with thread
     */
    public void run() {
        try {
            Thread.sleep(3000);//set for 3 seconds
        }
        catch (InterruptedException e) {
            System.out.println("InterruptedException");
            e.printStackTrace();
        }
        dismiss();
    }

    /**
     * A key release event triggers the dismiss()
     * method to be called.
     */
    public void keyReleased(int keyCode) {
        dismiss();
    }

    /**
     * A pointer release event triggers the dismiss()
     * method to be called.
     */
    public void pointerReleased(int x, int y) {
        dismiss();
    }
}

Possible enhancements/improvements:

  1. Create simple animation by displaying multiple images in sequence or just simply rotate same image
  2. Replace the image with Flash animation through libraries available in Project Capuchin

Full code can be downloaded from here.

Categories : Snippets, Tips, MobilePhone, JME

Export this post as PDF document  Export this post to PDF document

Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!



Add a comment Send a TrackBack