Header Sep
Java Tips, Tricks & Code
Score
Login to rate page
March 2004
Display a Java splash screen

[Back]


 

The startup time for advanced Java games/applications can be rather long, but by letting your MIDlet display a nice looking splash screen the user experience can usually be increased. However,  there might be more to that than you think.

If you simply try to display the splash screen as part of your main thread, you might run into trouble when you notice that the splash is not displayed until your MIDlet startup sequence has been executed. Or perhaps, it might not even show up at all!. This can happen if your code looks something like this:

    public void startApp() {
       myDisplay.setCurrent(mySplashScreen);
       doAllSlowInitializations();
       myDisplay.setCurrent(myGameScreen);
    }

The reason for this behavior is that the application manager doesn't make any Screen or Canvas visible until startApp() returns. See the MIDP Javadoc for Package javax.microedition.lcdui for more information.

The trick is that showing the splash screen and initializing the game should be done in separate threads, like shown in the code example below. This will allow the startApp() to return very quickly and the splash screen to be displayed as soon as possible. The two threads could simply  use a common variable to synchronize when the initialization is finished and then the splash screen should simply disappear. 

    public void startApp() {
        Thread splashScreenTest = new Thread(new SplashScreenTest.SplashScreen());
        splashScreenTest.start();
       
         Thread myGameThread = new Thread(this);
         myGameThread.start(); 
    }

The MIDlet below shows a working example of displaying a splash screen using this technique.

Another, more general technique to reduce the startup time is to use lazy loading, which means that you wait to initialize objects and resources until they are actually used. This technique can also have a very good impact on your MIDlet's memory needs, but the risks of getting a haltering application must be carefully considered.

SplashScreen.zip

  

 

Score
Login to rate page