Adding a splash screen
Written by: maffelu , 2009-07-17 14:15:18
package org.morkalork.splash;
import java.io.IOException;
import javax.microedition.lcdui.*;
import java.util.*;
/** * A splash screen! * @author maffelu */
public class SplashScreen extends Canvas{
//===================FIELDS=========================
private Display display;
private Displayable nextDisplayable;
private Timer timer;
private Image image = null;
//===================CONSTRUCTOR====================
/** * Creates a new SplashScreen object * @param display A Display object * @param nextDisplayable The next Displayable object to show (after splash screen) */
public SplashScreen(Display display, Displayable nextDisplayable){
this.display = display;
this.nextDisplayable = nextDisplayable;
timer = new Timer();
try{
//We've put the image in the base directory
image = Image.createImage("/org/morkalork/splash/MorkaSplash.jpg");
}
catch(IOException ioe){
System.out.println("Error: " + ioe.getMessage());
}
display.setCurrent(this);
}
/** * If a key is pressed, dismiss the splash screen * @param keyCode Any key */
protected void keyPressed(int keyCode){
dismiss();
}
/** * If a pointer is pressed, dismiss the splash screen * @param x The X coordinate * @param y The Y coordinate */
protected void pointerPressed(int x, int y){
dismiss();
}
/** * Dismisses the splash screen and shows the * next screen */
private void dismiss(){
timer.cancel();
display.setCurrent(nextDisplayable);
}
/** * The painting of the canvas, is never called directly but rather * by repaint. * @param g A Graphics object */
protected void paint(Graphics g){
g.setGrayScale(255);
g.fillRect(0, 0, g.getClipWidth() -1, g.getClipHeight() -1);
g.drawImage(image, 3, 10, Graphics.TOP | Graphics.LEFT);
}
/** * Showing will begin soon... */
protected void showNotify(){
timer.schedule(new CountDown(), 5000);
}
/** * A class to handle the dismissal of the splash screen * @author maffelu */
private class CountDown extends TimerTask{
/** * Dismisses the splash screen */
public void run(){
dismiss();
}
}
}
fillRect(X, Y, width, height);
setGrayScale(x);
protected void paint(Graphics g){
g.setGrayScale(255);
g.fillRect(0, 0, g.getClipWidth() -1, g.getClipHeight() -1);
g.drawImage(image, 3, 10, Graphics.TOP | Graphics.LEFT);
}
private Image image = null;
try{
//We've put the image in the base directory
image = Image.createImage("/org/morkalork/splash/MorkaSplash.jpg");
}
catch(IOException ioe){
System.out.println("Error: " + ioe.getMessage());
}
Error: null
java.lang.NullPointerException:
protected void showNotify(){
timer.schedule(new CountDown(), 5000);
}
private void dismiss(){
timer.cancel();
display.setCurrent(nextDisplayable);
}
package org.morkalork.splash;
import javax.microedition.lcdui.*;
/** * Displays a canvas with information about itself * @author maffelu */
public class CanvasScreen extends Canvas{
private Command cmdExit;
private int width;
private int height;
public Command getCmdExit(){
if(cmdExit == null){
cmdExit = new Command("Exit", Command.EXIT, 0);
}
return cmdExit;
}
/** * constructor */
public CanvasScreen(SplashMIDlet parent) {
width = this.getWidth();
height = this.getHeight();
this.addCommand(getCmdExit());
this.setCommandListener(parent);
}
/** * Paint a new screen */
public void paint(Graphics g) {
//Set the drawing color to white
g.setGrayScale(255);
//Draw a big white rectangle over the whole screen (over the previous splash screen)
g.fillRect(0, 0, width -1, height -1);
//Set the drawing color to black
g.setGrayScale(0);
//Output text (3 pixels from the left)
g.drawString("Canvas information:",3,0,Graphics.TOP|Graphics.LEFT);
g.drawString("Width: " + width, 3, 12, Graphics.TOP|Graphics.LEFT);
g.drawString("Height: " + height, 3, 24, Graphics.TOP|Graphics.LEFT);
}
}
package org.morkalork.splash;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/** * Displays a splash screen and a Form * @author maffelu */
public class SplashMIDlet extends MIDlet implements CommandListener{
private Display display;
private Command cmdExit;
private CanvasScreen canvasScreen;
/** * Empty constructor */
public SplashMIDlet(){
canvasScreen = new CanvasScreen(this);
}
/** * Switches the current screen to a new screen * @param alert An Alert screen, or NULL * @param next The new display object */
public void switchDisplay(Alert alert, Displayable next){
display = Display.getDisplay(this);
if(alert == null){
display.setCurrent(next);
} else {
display.setCurrent(alert, next);
}
}
/** * Starts the MIDlet by showing the splash screen */
public void startMIDlet(){
cmdExit = new Command("Exit", Command.EXIT, 0);
display = Display.getDisplay(this);
switchDisplay(null, new SplashScreen(display, canvasScreen));
}
/** * Exits the MIDlet */
public void exitMIDlet(){
notifyDestroyed();
}
public void commandAction(Command command, Displayable displayable){
if(displayable == canvasScreen){
if(command == canvasScreen.getCmdExit()){
exitMIDlet();
}
}
}
public void startApp() {
if(display == null){
startMIDlet();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
switchDisplay(null, new SplashScreen(display, canvasScreen));
There is 1 comment on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
h34rth4ck3r
2011-10-15 09:47:34
this is pretty cool. Ill use this to display 3 splash screen :-) .
I Have a question, how do you create Main Menu on game? which is Based by the Canvas class and not by the List class. Just like Gameloft games, I think they drawed their menu by using the paint method of the Canvas class. Please send me an email on how I do it. tHAnks