example.pushpuzzle
Class PushPuzzleCanvas

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by javax.microedition.lcdui.game.GameCanvas
              extended by example.pushpuzzle.PushPuzzleCanvas
All Implemented Interfaces:
java.lang.Runnable

 class PushPuzzleCanvas
extends javax.microedition.lcdui.game.GameCanvas
implements java.lang.Runnable

PushPuzzleCanvas displays the game board and handles key events. The PushPuzzle game logic and algorithms are separated into Board.java. PushPuzzleCanvas does not setup or use any Commands. Commands for each screen and listeners should be setup outside this class. PushPuzzleCanvas generates a SELECT_COMMAND when the current level is solved. Sequencing through screens is done in the PushPuzzle MIDlet.

PushPuzzleCanvas handles the reading, initialization, and sequencing of individual puzzle screens.

PushPuzzleCanvas uses the Score class to restore and save game levels and scores for each level. To display the scores use getScoreScreen. It will be initialized with the current scores. To select a new level use the getLevelScreen and gotoLevel methods.

PushPuzzleCanvas handled key events for LEFT, RIGHT, UP, and DOWN to move the pusher in the game board. Pointer pressed events are used to move the pusher to the target location (if possible).


Field Summary
 int TILE_GROUND
          The index in the image of the Ground
 int TILE_PACKET
          The index in the image of the Packet
 int TILE_PUSHER
          The index in the image of the Pusher
 int TILE_STORE
          The index in the image of the Store
 int TILE_WALL
          The index in the image of the Wall
 
Fields inherited from class javax.microedition.lcdui.game.GameCanvas
DOWN_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, UP_PRESSED
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
PushPuzzleCanvas(PushPuzzle pushpuzzle, Score s)
          Construct a new canvas
 
Method Summary
 void changeTheme()
          Change themes.
(package private)  void closePlayer()
           
 void destroy()
          Cleanup and destroy.
 int getLevel()
          Get the current level.
 javax.microedition.lcdui.Screen getLevelScreen()
          Get a screen to let the user change the level.
 javax.microedition.lcdui.Screen getScoreScreen()
          Return the Screen to display scores.
 boolean gotoLevel()
          Go to the chosen Level.
protected  void hideNotify()
          The canvas is being removed from the screen.
 void init()
          Read the previous level number from the score file.
protected  void keyPressed(int keyCode)
          Handle a single key event.
protected  void keyRepeated(int keyCode)
          Handle a repeated arrow keys as though it were another press.
 boolean nextLevel(int offset)
          Start the next level.
 void paint(javax.microedition.lcdui.Graphics g)
          Paint the contents of the Canvas.
(package private)  void play(byte[] tune)
          Play the simple tune supplied.
protected  void pointerPressed(int x, int y)
          Called when the pointer is pressed.
 void restartLevel()
          Restart the current level.
 void run()
          The main event processor.
 void setCommandListener(javax.microedition.lcdui.CommandListener l)
          Add a listener to notify when the level is solved.
protected  void showNotify()
          The canvas is being displayed.
 void undoMove()
          Undo the last move if possible.
(package private)  void updateTilesNear(int loc, int dir)
          Queue a repaint for an area around the specified location.
 
Methods inherited from class javax.microedition.lcdui.game.GameCanvas
flushGraphics, flushGraphics, getGraphics, getKeyStates
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, isDoubleBuffered, keyReleased, pointerDragged, pointerReleased, repaint, repaint, serviceRepaints, setFullScreenMode, sizeChanged
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setTicker, setTitle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TILE_GROUND

public final int TILE_GROUND
The index in the image of the Ground

See Also:
Constant Field Values

TILE_PACKET

public final int TILE_PACKET
The index in the image of the Packet

See Also:
Constant Field Values

TILE_STORE

public final int TILE_STORE
The index in the image of the Store

See Also:
Constant Field Values

TILE_WALL

public final int TILE_WALL
The index in the image of the Wall

See Also:
Constant Field Values

TILE_PUSHER

public final int TILE_PUSHER
The index in the image of the Pusher

See Also:
Constant Field Values
Constructor Detail

PushPuzzleCanvas

public PushPuzzleCanvas(PushPuzzle pushpuzzle,
                        Score s)
Construct a new canvas

Parameters:
pushpuzzle - the main MIDlet
s - the score object
Method Detail

init

public void init()
Read the previous level number from the score file. Read in the level data.


destroy

public void destroy()
Cleanup and destroy.


changeTheme

public void changeTheme()
Change themes. Cycle to the next index and try it


undoMove

public void undoMove()
Undo the last move if possible. Redraw the cell the pusher occupies after the undone move and the cells in the direction of the original move. Here so undo can be triggered by a command.


restartLevel

public void restartLevel()
Restart the current level.


nextLevel

public boolean nextLevel(int offset)
Start the next level.

Parameters:
offset - of the next level
Returns:
true if the new level was loaded

getLevel

public int getLevel()
Get the current level.

Returns:
the current level.

getLevelScreen

public javax.microedition.lcdui.Screen getLevelScreen()
Get a screen to let the user change the level. A simple numeric TextBox will do.

Returns:
the textbox used to change the level number

gotoLevel

public boolean gotoLevel()
Go to the chosen Level.

Returns:
true if the new level was loaded.

getScoreScreen

public javax.microedition.lcdui.Screen getScoreScreen()
Return the Screen to display scores. It returns a screen with the current scores.

Returns:
a screen initialized with the current score information.

keyRepeated

protected void keyRepeated(int keyCode)
Handle a repeated arrow keys as though it were another press.

Overrides:
keyRepeated in class javax.microedition.lcdui.Canvas
Parameters:
keyCode - the key pressed.

keyPressed

protected void keyPressed(int keyCode)
Handle a single key event. The LEFT, RIGHT, UP, and DOWN keys are used to move the pusher within the Board. Other keys are ignored and have no effect. Repaint the screen on every action key.

Overrides:
keyPressed in class javax.microedition.lcdui.Canvas

pointerPressed

protected void pointerPressed(int x,
                              int y)
Called when the pointer is pressed. Record the target for the pusher.

Overrides:
pointerPressed in class javax.microedition.lcdui.Canvas
Parameters:
x - location in the Canvas
y - location in the Canvas

setCommandListener

public void setCommandListener(javax.microedition.lcdui.CommandListener l)
Add a listener to notify when the level is solved. The listener is send a List.SELECT_COMMAND when the level is solved.

Overrides:
setCommandListener in class javax.microedition.lcdui.Displayable
Parameters:
l - the object implementing interface CommandListener

updateTilesNear

void updateTilesNear(int loc,
                     int dir)
Queue a repaint for an area around the specified location.

Parameters:
loc - an encoded location from Board.getPusherLocation
dir - that the pusher moved and flag if it pushed a packet

paint

public void paint(javax.microedition.lcdui.Graphics g)
Paint the contents of the Canvas. The clip rectangle of the canvas is retrieved and used to determine which cells of the board should be repainted.

Overrides:
paint in class javax.microedition.lcdui.game.GameCanvas
Parameters:
g - Graphics context to paint to.

showNotify

protected void showNotify()
The canvas is being displayed. Stop the event handling and animation thread.

Overrides:
showNotify in class javax.microedition.lcdui.Canvas

hideNotify

protected void hideNotify()
The canvas is being removed from the screen. Stop the event handling and animation thread.

Overrides:
hideNotify in class javax.microedition.lcdui.Canvas

run

public void run()
The main event processor. Events are polled and actions taken based on the directional events.

Specified by:
run in interface java.lang.Runnable

play

void play(byte[] tune)
Play the simple tune supplied.


closePlayer

void closePlayer()