日期:2014-05-20  浏览次数:20630 次

请教:一个关于线程的问题
请看下面的代码:
import   java.util.Random;
import   javax.microedition.lcdui.*;
import   javax.microedition.lcdui.game.GameCanvas;

public   class   MyCanvas   extends   GameCanvas   implements   Runnable{
    private   Graphics   graphics;
    private   int   width,   height;
    Thread   thread   =   new   Thread(new   MyCanvas());

    protected   MyCanvas()   {
          super(true);
          graphics   =   this.getGraphics();
          width   =   getWidth();
          height   =   getHeight();
          thread.start();
    }

    public   void   paint(Graphics   g)   {
          setFullScreenMode(true);
          g.setColor(0x000000);
          g.fillRect(0,   0,   getWidth(),   getHeight());
          drawBackground(g);
          drawHeart(g);
    }

    static   Image   createImage(String   imageName){
          Image   img   =   null;
          try{
                    img   =   Image.createImage(imageName);
          }catch(Exception   e){
          }
                    return   img;  
    }

    public   void   drawBackground(Graphics   g){
          Image   background   =   null;
          background   =   createImage( "/image/bg_0.png ");
          g.drawImage(background,   background.getWidth()   /   2,   background.getHeight()   /   2,   Graphics.HCENTER|Graphics.VCENTER);
    }

    public   void   drawSky(Graphics   g){
          Image   sky   =   null;
          sky   =   createImage( "/image/shanxing.png ");
          sky.getGraphics();
          g.drawImage(sky,   sky.getWidth()   /   2,   sky.getHeight()   /   2,   Graphics.HCENTER|Graphics.VCENTER);
    }

          .........

    public   void   run()   {
          boolean   isRunning   =   true;
          while(isRunning){
                    drawSky(graphics);
                    try   {
                              thread.sleep(80);
                    }   catch   (InterruptedException   e)   {
                              e.printStackTrace();