#ff0000 21  2005 . - #000655 MIDP  API  
#def  API     .    ,        .            .        .     API   ,        .       ,           .   ,    MIDP  ,    ,   API,   .   ,     .
   API,    Canvas:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class MyCanvas extends Canvas {

  private MIDlet midlet;

   public MyCanvas( MIDlet midlet ){
     this.midlet = midlet;
   } 

   protected void paint( Graphics g ){
     g.setColor( 255, 255, 255 );
     g.fillRect( 0, 0, getWidth(), getHeight() );
     g.setColor( 0, 0, 0 );
     g.drawString( "Hello there!", getWidth()/2, 0,g.TOP | g.HCENTER );
   }
}
       javax.microedition.lcdui.      javax.microedition.midlet,     canvas   MIDlet.   canvas      paint.    ,    .  paint   Graphics,          , : drawArc, drawLine, drawRect,  drawString. API  24-bit RGB  ,   ,    ,     .   MyCanvas            .
Canvas    setCurrent  Display.      startApp:
import javax.microedition.midlet.*;

public class MyMIDlet extends MIDlet {

   private Display display;
   private MyCanvas canvas;

   public MyMIDlet(){
     display = Display.getDisplay( this );
     canvas = new MyCanvas( this );
   }

   protected void startApp(){
     display.setCurrent( canvas );
   }

   protected void pauseApp(){
   }

   protected void destroyApp( boolean unconditional ){
   }

   public void exit(){
     destroyApp( true );
     notifyDestroyed();
   }
}
     :       . ,   -    .   :    (raw input events)    (command events).
     ,      Canvas.        : 
   (keyPressed, keyRepeated  keyReleased) ; 
  (pointerPressed, pointerDragged  pointerReleased),    ; 
 canvas (showNotify, hideNotify). 
,     ,   keyPressed.

protected void keyPressed( int keyCode ){
  ((MyMIDlet) midlet).exit();
}
      keyCode,    ,  .        Unicode,   -  ,        .         ,   Canvas   ,      . ,  ,    (UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B, GAME_C  GAME_D).           Canvas.getGameAction    .
     :
public abstract class GameCanvas extends Canvas {
   protected MIDlet midlet;
   protected int fireKey;
   protected int leftKey;
   protected int rightKey;
   protected int upKey;
   protected int downKey;

   public GameCanvas( MIDlet midlet ){
     this.midlet = midlet;
     fireKey = getKeyCode( FIRE );
     leftKey = getKeyCode( LEFT );
     rightKey = getKeyCode( RIGHT );
     upKey = getKeyCode( UP );
     downKey = getKeyCode( DOWN );
   }
}
     :
public class MyCanvas extends GameCanvas {
   private String message = "Press any key";

   public MyCanvas( MIDlet midlet ){
     super( midlet );
   }

   protected void paint( Graphics g ){
     g.setColor( 255, 255, 255 );
     g.fillRect( 0, 0, getWidth(), getHeight() );
     g.setColor( 0, 0, 0 );
     g.drawString( message, getWidth()/2, 0,
       g.TOP | g.HCENTER );
   }

   protected void keyPressed( int keyCode ){
     if( keyCode == fireKey ){
       message = "FIRE";
     } else if( keyCode == leftKey ){
       message = "LEFT";
     } else if( keyCode == rightKey ){
       message = "RIGHT";
     } else if( keyCode == upKey ){
       message = "UP";
     } else if( keyCode == downKey ){
       message = "DOWN";
     } else {
       message = getKeyName( keyCode );
     }

     repaint();
   }
}
   ,    MIDP   .        ,   Canvas.hasPointerEvents.     :

protected void pointerPressed( int x, int y ){
   //  
} 

      ""   canvas-.  -    .      ,   .   ,        . ,      ,    OK ,         .   : BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN  STOP.      .        ,      .    - 1.
     Command:
Command exitCommand = new Command( "Exit", Command.SCREEN, 1 ); 
    ,   addCommand method:
canvas.addCommand( exitCommand ); 
    :
setListener: canvas.setListener( listener );
   CommandListener .       MIDLet-:
import javax.microedition.midlet.*;
public class MyMIDlet extends MIDlet implements 
   CommandListener {

   private Display display;
   private MyCanvas canvas;
   private Command exitCommand = new Command( 
     "Exit", Command.SCREEN, 1 );

   public MyMIDlet(){
     display = Display.getDisplay( this );
     canvas = new MyCanvas( this );
     canvas.addCommand( exitCommand );
     canvas.setListener( this );
   }

   protected void startApp(){
     display.setCurrent( canvas );
   }

   protected void pauseApp(){
   }

   protected void destroyApp( boolean unconditional ){
   }

   public void exit(){
     destroyApp( true );
     notifyDestroyed();
   }
   public void commandAction( Command c, Displayable d ){
     if( c == exitCommand ){
       exit();
     }
   }
}
 CommandListener    commandAction,     .   Command       ,         .
#0000ff   Eric Giguere .
#0000ff : aRix