FileDocCategorySizeDatePackage
PushPuzzle.javaAPI DocJ2ME MIDP 2.04982Thu Nov 07 12:02:20 GMT 2002example.pushpuzzle

PushPuzzle

public class PushPuzzle extends MIDlet implements CommandListener
PushPuzzle is the MIDlet that drives the game. It puts up the screens and handles all the commands that are invoked on each screen.

Fields Summary
Display
display
private PushPuzzleCanvas
canvas
private Score
score
private Screen
scoreScreen
private Screen
levelScreen
private Alert
alert
private Command
undoCommand
private Command
restartCommand
private Command
exitCommand
private Command
scoresCommand
private Command
okCommand
private Command
levelCommand
private Command
nextCommand
private Command
prevCommand
private Command
aboutCommand
private Command
themeCommand
Constructors Summary
public PushPuzzle()
Creates new PushPuzzle MIDlet.


             
      
	display = Display.getDisplay(this);
	score = new Score();
	canvas = new PushPuzzleCanvas(this, score);
	alert = new Alert("Warning");
    
Methods Summary
public voidcommandAction(Command c, Displayable s)
Respond to a commands issued on any Screen

	if (c == undoCommand) {
	    canvas.undoMove();
	} else if (c == restartCommand) {
	    canvas.restartLevel();
	} else if (c == levelCommand) {
	    levelScreen = canvas.getLevelScreen();
	    levelScreen.addCommand(okCommand);
	    levelScreen.setCommandListener(this);
	    display.setCurrent(levelScreen);
	} else if (c == okCommand && s == levelScreen) {

	    if (!canvas.gotoLevel()) {
		alert.setString("Could not load level");
		display.setCurrent(alert, canvas);
	    } else {
		display.setCurrent(canvas);
	    }
	} else if (c == scoresCommand) {
	    scoreScreen = canvas.getScoreScreen();
	    scoreScreen.addCommand(okCommand);
	    scoreScreen.setCommandListener(this);
	    display.setCurrent(scoreScreen);
	} else if (c == okCommand && s == scoreScreen) {
	    display.setCurrent(canvas);
	} else if (c == exitCommand) {
	    destroyApp(false);
	    notifyDestroyed();
	} else if (c == List.SELECT_COMMAND && s == canvas) {
	    // Solved the level
	    scoreScreen = canvas.getScoreScreen();
	    scoreScreen.addCommand(okCommand);
	    scoreScreen.setCommandListener(this);
	    display.setCurrent(scoreScreen);

	    // Read the next screen.
	    canvas.nextLevel(1);
	} else if (c == nextCommand) {
	    if (!canvas.nextLevel(1)) {
		alert.setString("Could not load level " +
				(canvas.getLevel() + 1));
		display.setCurrent(alert, canvas);
	    } else {
		display.setCurrent(canvas);
	    }
            if (s == canvas) {
                canvas.repaint();
            }
	} else if (c == prevCommand) {
	    if (!canvas.nextLevel(-1)) {
		alert.setString("Could not load level " +
				(canvas.getLevel() - 1));
		display.setCurrent(alert, canvas);
	    } else {
		display.setCurrent(canvas);
	    }
            if (s == canvas) {
                canvas.repaint();
            }
	} else if (c == aboutCommand) {
	    About.showAbout(display);
	} else if (c == themeCommand) {
	    canvas.changeTheme();
	}
    
public voiddestroyApp(boolean unconditional)
Destroy must cleanup everything. Only objects exist so the GC will do all the cleanup after the last reference is removed.

	display.setCurrent((Displayable)null);
	canvas.destroy();
	if (score != null)
	    score.close();
    
public voidpauseApp()
Pause signals the thread to stop by clearing the thread field. If stopped before done with the iterations it will be restarted from scratch later.

    
public voidstartApp()
Start creates the thread to do the timing. It should return immediately to keep the dispatcher from hanging.

	if (!score.open()) {
	    System.out.println("Score open failed");
	}

	canvas.init();
	canvas.addCommand(undoCommand);
	canvas.addCommand(scoresCommand);
	canvas.addCommand(restartCommand);
	canvas.addCommand(levelCommand);
	canvas.addCommand(exitCommand);
	canvas.addCommand(nextCommand);
	canvas.addCommand(prevCommand);
	canvas.addCommand(aboutCommand);
	canvas.addCommand(themeCommand);
	canvas.setCommandListener(this);

	display.setCurrent(canvas);