#ff0000 6  2005 . - #000655 J2ME.         Canvas 
#def        
      -   .          .      ,      .         ,         -   ,      canvas.
         .   .
       canvas  :

import javax.microedition.lcdui.*;

public class MyCanvas extends Canvas implements Runnable {

public MyCanvas(Midlet midlet) { // 
    }

public void run(){ 
     try {
       while (running) {
        //  
       }
     }
     catch(InterruptedException ie) { System.out.println(ie.toString()); }
}
protected void paint(Graphics g){ /* code */ }

synchronized void start() { /* code */ }

synchronized void stop() { /* code */ }

public void keyPressed(int keyCode) { /* code */ }
}
      canvas.  -       .        canvas  -        .
   Menu.java  Game.java.      ,     .  ,   Canvas.  ,           ,        ,    .       Canvas ,     . 
public class Menu{
   boolean isActive;

   public static boolean isActive() { return isActive; }

   public static void destroy() { /*      */ }

   public static void initMenu() { /*   */ }

  private static void initRest(){ /*     */ }

  public static void paint(Graphics g) { 
     isActive = true;

     drawMenuBackground(g);

     switch (menuType) {
         case 1: showMenuA(g); break;
         case 2: showMenuB(g); break; 
     }

   }

  public static void processKey(int keyCode, int GameActionKey) { /*   */   }
} 

public class Game {

  boolean isActive;

  public static boolean isActive() { return isActive; }

  public static void destroy() { /*    */ }

  public static void initGame() { /*   */ }

  public static void paint(Graphics g) { 
    //  
    isActive = true;
   }

   public static void processKey(int keyCode, int GameActionKey) { /*  */ }
} 
         MyCanvas.
   ,   canvas-      .    boolean,      (  ).
   MyCanvas,        MotorDuels: Outcast.    Canvas.     Nokia,    FullCanvas,     DirectGraphics.  ,      dg   g  Game.paint()  Menu.paint().
import javax.microedition.lcdui.*;

public class MyCanvas extends Canvas implements Runnable {


   private volatile Thread animationThread=null;

   private static Graphics graphics;
   private static boolean running;


   public static boolean inMenu = true;
   public static boolean doInit = true;

   private static final int SLEEP = 5; 


   public MyCanvas(MyGame mygame) {
      buffer=Image.createImage(MyGame.canvasWidth, MyGame.canvasHeight);
      graphics=buffer.getGraphics();
   }


   public void run(){ //main_game_loop
      try {
         while (running) {

            if (doInit){
               doInit=false;
               if (inMenu) Menu.initMenu();
               else Game.initGame();
            }

            if (inMenu) {
               if (!Menu.isActive()) {
                  doInit=true;
                  inMenu=false;
                  Menu.destroy();
                  Sounds.destroyMenu();
               }
            } else {
            if (!Game.isActive()) {
               inMenu = true;
               doInit = true;
               Game.destroy();
               }

            }

            repaint(0,0,MyGame.canvasWidth, MyGame.canvasHeight);
            serviceRepaints();
            Thread.sleep(SLEEP);
         }
      }
   catch(InterruptedException ie) { System.out.println(ie.toString()); }
}


protected void paint(Graphics g){



   if (inMenu) Menu.paint(g);
   else Game.paint(g); 

}


   synchronized void start()
   {
      running=true;
      animationThread=new Thread(this);
      animationThread.start();
   }

synchronized void stop() { running=false; }


public void keyPressed(int keyCode) {
   if (allowKeys) {
      if (inMenu) {
         Menu.processKey(keyCode, getGameAction(keyCode));
      }
      else {
         Game.processKey(keyCode, getGameAction(keyCode));
      }
   }

}

}
 ,         paint()  ,        init().      keypressed(),        .
    ?
              .  ,        jar ,     .
  , ,    ,  .    ,         .  ,         . 
,         .         ,           . ,    .