JME(J2ME) Splash Screen

peter_budo 6 Tallied Votes 699 Views Share

Here is simple example of static image to be displayed as logo on the start-up/splash screen.
This can be improved by creating animation out of series of images or use of flash file through Project Capuchin library.
As for location of image file this was placed in new folder called "res" as resources. The folder is on same level as "myProject" folder that holds Java classes. If you decide to have "res" folder placed inside "myProject" folder then image path will be /myProject/res/IMAGE_NAME ProjectMIDlet.java

package myProject;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 * URL: [url]http://www.peterscorner.co.uk[/url]
 * Date: 02-Oct-2009
 * Time: 17:40:55
 */

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;

import myProject.screens.SplashScreen;

public class ProjectMIDlet extends MIDlet{

    public String ti;
    public String entrydate;
    public String result;

    private Display display;


    public ProjectMIDlet() {}

    public void startApp() {
        if(display == null){
            display = Display.getDisplay(this);
        }
        display.setCurrent(new SplashScreen(this));

    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

}

NextScreen.java

package myProject.screens;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 * URL: [url]http://www.peterscorner.co.uk[/url]
 * Date: 02-Oct-2009
 * Time: 17:56:08
 */
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import myProject.ProjectMIDlet;

public class NextScreen extends Form implements CommandListener{

    private ProjectMIDlet projectMidlet;

    public NextScreen(ProjectMIDlet projectMidlet){
        super("Next Screen");
        this.projectMidlet = projectMidlet;
        init();
    }

    private void init(){
        StringItem si = new StringItem(null, "Next Screen");
        append(si);
    }

    public void commandAction(Command c, Displayable d){}
}
majestic0110 commented: Great :) Nice snippet! +6
jasimp commented: Dani would be proud, you tagged it wonderfully :) +12
justM commented: Helped me twice on this +1
package myProject.screens;

/**
 * Created by IntelliJ IDEA.
 * User: Peter
 * URL: http://www.peterscorner.co.uk
 * Date: 02-Oct-2009
 * Time: 17:46:18
 */
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 myProject.ProjectMIDlet;

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

    private Image mImage;
    private ProjectMIDlet projectMIDlet;

    /**
     * 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(ProjectMIDlet projectMIDlet){
        this.projectMIDlet = projectMIDlet;
        try{
        mImage = Image.createImage("/res/badge.jpg");
        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(projectMIDlet).setCurrent(new NextScreen(projectMIDlet));
    }

    /**
     * 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();
    }
}