net.kbox.component
Class KGameCanvas

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by javax.microedition.lcdui.game.GameCanvas
              extended by net.kbox.component.KGameCanvas
All Implemented Interfaces:
Unique

public abstract class KGameCanvas
extends javax.microedition.lcdui.game.GameCanvas
implements Unique

The event model of GameCanvas is stinking: is A canvas worth of a thread? It seems sometime we have to face with so many thread which used to control GameCanvas,or be busying to decide which GameCanvas to be listening.KGameCanvas is design to solve such problems. It rewrite keypressed method inherit from Canvas. All the keypressed event will be receive by a ActionController which is the command listener too.
All the key event was map with a name:
Canvas.UP: "UP"
Canvas.DOWN:"DOWN"
Canvas.LEFT:"LEFT"
Canvas.RIGHT:"RIGHT"
Canvas.FIRE:"FIRE"
Canvas.KEY_NUM0:"0"
Canvas.KEY_NUM1:"1"
Canvas.KEY_NUM2:"2"
Canvas.KEY_NUM3:"3"
Canvas.KEY_NUM4:"4"
Canvas.KEY_NUM5:"5"
Canvas.KEY_NUM6:"6"
Canvas.KEY_NUM7:"7"
Canvas.KEY_NUM8:"8"
Canvas.KEY_NUM9:"9"
Canvas.KEY_NUM#:"#"
Canvas.KEY_NUM*:"*"
These mapping name is used for mapping when forward the event.
If a key pressed event was needed to deal, for example, LEFT key pressed, it can be deal like this way:

1.Specify the action for dealing this key pressed event in kbox.cfg.property:
...
KEY.[Container ID].LEFT.ACTION=foo.boo.LeftAction
...
[Container ID] is the KGameCanvas which contain product this key pressed event.

2.Design your deal class:
public LeftAction implements Action{
public void excute(KCommand cmd, Displayable dis, ActionForward forward,Hashtable parameters){
System.out.println("Left hello!");
}
}

KGameCanvas also have dealt with the repeat key pressed event.

Author:
blackBox

Field Summary
protected  KCommand[] cmds
          kcommands added to KGameCanvas
protected  ActionController controller
          the event forward controller.
protected  boolean isPressed
          true if a key is pressed.
protected  boolean isThreadSafe
           
protected  int keyPressedDelay
          keyPressedDelay representing how sensitive the KGameCanvas is.
protected  int keyPressedDelayDESC
          keyPressedDelayDESC use for reducing the value of keyPressedDelay.
protected  int minDelay
          minDelay specified the min value of keyPressedDelay.
protected  java.lang.String uniqueID
          uniqueID used for mapping.
 
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
KGameCanvas(java.lang.String uniqueID)
          Create a new KGameCanvas.
KGameCanvas(java.lang.String uniqueID, int keyPressedDelay, int keyPressedDelayDESC, int minDelay)
          Create a KGameCanvas with specified sensitive value.
KGameCanvas(java.lang.String uniqueID, int keyPressedDelay, int keyPressedDelayDESC, int minDelay, boolean isThreadSafe)
          Create a KGameCanvas with specified sensitive value.
 
Method Summary
 void addCommand(javax.microedition.lcdui.Command cmd)
          Over write addCommand() to make sure that KCommands with same uniqueID will be added just once.
 void addCommand(javax.microedition.lcdui.Command cmd, boolean isUnique)
          Add a command.
 KCommand[] getKCommand(java.lang.String uniqueId)
          Get KCommand with specified uniqueId added to KGameCanvas
 java.lang.String getUniqueID()
          Return a Unique id used to identify a component.
 boolean isThreadSafe()
           
protected  void keyPressed(int keyCode)
          keyPressed event method.
protected  void keyReleased(int keyCode)
          stop repeat key pressed event.
protected  java.lang.String mapKeyCode(int keyCode)
          Mapping keyCode to a name.
 void removeAllKCommand()
          Remove all KCommand;
 KCommand[] removeKCommand(java.lang.String uniqueId)
          Remove KCommand with specified uniqueId.
 void setCommandListener(javax.microedition.lcdui.CommandListener listener)
          Bind to a Action Controller.
 void setThreadSafe(boolean isThreadSafe)
           
 void setUniqueID(java.lang.String uniqueID)
           
 
Methods inherited from class javax.microedition.lcdui.game.GameCanvas
flushGraphics, flushGraphics, getGraphics, getKeyStates, paint
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotify, isDoubleBuffered, keyRepeated, pointerDragged, pointerPressed, pointerReleased, repaint, repaint, serviceRepaints, setFullScreenMode, showNotify, sizeChanged
 
Methods inherited from class javax.microedition.lcdui.Displayable
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

uniqueID

protected java.lang.String uniqueID
uniqueID used for mapping.


controller

protected ActionController controller
the event forward controller.


isPressed

protected boolean isPressed
true if a key is pressed.


keyPressedDelay

protected int keyPressedDelay
keyPressedDelay representing how sensitive the KGameCanvas is. Lesser the value is, more sensitive it is.
Default value is 85.


keyPressedDelayDESC

protected int keyPressedDelayDESC
keyPressedDelayDESC use for reducing the value of keyPressedDelay. Default value is 5.


minDelay

protected int minDelay
minDelay specified the min value of keyPressedDelay. Defatul value is 45.


cmds

protected KCommand[] cmds
kcommands added to KGameCanvas


isThreadSafe

protected boolean isThreadSafe
Constructor Detail

KGameCanvas

public KGameCanvas(java.lang.String uniqueID)
Create a new KGameCanvas.

Parameters:
uniqueID -

KGameCanvas

public KGameCanvas(java.lang.String uniqueID,
                   int keyPressedDelay,
                   int keyPressedDelayDESC,
                   int minDelay)
Create a KGameCanvas with specified sensitive value. It equals KGameCanvas(uniqueID, keyPressedDelay, keyPressedDelayDESC, minDelay, true).

Parameters:
uniqueID -
keyPressedDelay -
keyPressedDelayDESC -
minDelay -

KGameCanvas

public KGameCanvas(java.lang.String uniqueID,
                   int keyPressedDelay,
                   int keyPressedDelayDESC,
                   int minDelay,
                   boolean isThreadSafe)
Create a KGameCanvas with specified sensitive value. If need a thread safe KGameCanvas, set isThreadSafe to true, and synchronize the code with KGameCanvas object.

Parameters:
uniqueID -
keyPressedDelay -
keyPressedDelayDESC -
minDelay -
isThreadSafe -
Method Detail

addCommand

public void addCommand(javax.microedition.lcdui.Command cmd)
Over write addCommand() to make sure that KCommands with same uniqueID will be added just once.

Overrides:
addCommand in class javax.microedition.lcdui.Displayable

addCommand

public void addCommand(javax.microedition.lcdui.Command cmd,
                       boolean isUnique)
Add a command. If isUnique is true, it will replace the old command which has the same uniqueId; if false, the command just be added.

Parameters:
cmd - the command to be added.
isUnique - if is a unique command.

getKCommand

public KCommand[] getKCommand(java.lang.String uniqueId)
Get KCommand with specified uniqueId added to KGameCanvas

Parameters:
uniqueId - uniqueId of a KCommand
Returns:
a KCommand, or null if no KCommand with the uniqueId in KGameCanvas

removeKCommand

public KCommand[] removeKCommand(java.lang.String uniqueId)
Remove KCommand with specified uniqueId.

Parameters:
uniqueId - uniqueId of a KCommand
Returns:
the deleted KCommand, or null if no KCommand with the uniqueId

removeAllKCommand

public void removeAllKCommand()
Remove all KCommand;


keyPressed

protected void keyPressed(int keyCode)
keyPressed event method. It will create a new thread to repeat the event until a keyReleased event occur or the container turn into invisible.

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

mapKeyCode

protected java.lang.String mapKeyCode(int keyCode)
Mapping keyCode to a name.

Parameters:
keyCode -

keyReleased

protected void keyReleased(int keyCode)
stop repeat key pressed event.

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

setCommandListener

public void setCommandListener(javax.microedition.lcdui.CommandListener listener)
Bind to a Action Controller.

Overrides:
setCommandListener in class javax.microedition.lcdui.Displayable

isThreadSafe

public boolean isThreadSafe()

setThreadSafe

public void setThreadSafe(boolean isThreadSafe)

setUniqueID

public void setUniqueID(java.lang.String uniqueID)

getUniqueID

public java.lang.String getUniqueID()
Description copied from interface: Unique
Return a Unique id used to identify a component.

Specified by:
getUniqueID in interface Unique