MyCanvas.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.lcdui.game.*;
  3. import java.io.IOException;
  4. public class MyCanvas extends GameCanvas implements Runnable,CommandListener  
  5. {
  6.   private TiledLayerDemo midlet;//程序控制类
  7.   private volatile Thread animationThread=null;//线程
  8.   private TiledLayer mBackground;//地图
  9.     private LayerManager mLayerManager;
  10.   private int canvasHeight, canvasWidth;//屏幕高度/宽度
  11.   private static int tileHeight,tileWidth;//地图的高度/
  12.   private static int columns,rows;
  13.   private static int mapWidth,mapHeight;
  14.   private int MON = 1;//月亮moon
  15.   private int SHN = 2;//闪烁shine
  16.   private int STR = 3;//星星star
  17.   private int AIR = 4;//夜空air
  18.   private int ANI;//动画贴片animated
  19.   
  20.   private int aniStarIndex;
  21.   private int aniCounter;
  22.   private boolean isRun;  
  23.   private int curX,curY;//当前viewer坐标
  24.   
  25.   private final int SLEEP=10;
  26.   private Command exit;
  27.   public MyCanvas(TiledLayerDemo midlet)
  28.   {
  29.     super(true);
  30.     this.midlet = midlet;
  31.     canvasHeight=getHeight();
  32.     canvasWidth=getWidth();
  33.     try{
  34.     mBackground = createBackground();//创建背景
  35.     }catch(IOException e){
  36.     System.out.println("create error");
  37.     }
  38.     curX = -(mapWidth - canvasWidth)/2;
  39.     curY = -(mapHeight - canvasHeight)/2;
  40.     mBackground.setPosition(curX,curY);  
  41.     exit=new Command("Exit", Command.STOP,1);
  42.     this.addCommand(exit);
  43.     this.setCommandListener(this);//两个按键
  44.    }
  45.  
  46.   synchronized void start()
  47.   {
  48.     isRun=true;
  49.     animationThread=new Thread(this);
  50.     animationThread.start();
  51.   }
  52.   public void run()
  53.   {
  54.     Graphics g = this.getGraphics();
  55.     try
  56.     {
  57.       while (isRun)
  58.       {
  59.        tick();
  60.        render(g);
  61.         Thread.sleep(SLEEP);
  62.       }
  63.     }
  64.     catch(InterruptedException ie)
  65.     {
  66.       System.out.println(ie.toString());
  67.     }
  68.   }
  69.   private void tick(){
  70.      aniCounter++;
  71.      if(aniCounter%6 == 0)
  72.      mBackground.setAnimatedTile(ANI,SHN);
  73.      else
  74.      mBackground.setAnimatedTile(ANI,STR);
  75.   }
  76.  
  77.   private TiledLayer createBackground()throws IOException {
  78.     Image image = Image.createImage("/star.png");//创建背景图片
  79.     TiledLayer tiledLayer = new TiledLayer(10, 10, image, 32, 32);//注意:背景的参数的设置!
  80.     tileWidth = 32;
  81.     tileHeight = 32; 
  82.     columns = 10;
  83.     rows = 10;
  84.     mapWidth =tileWidth*columns;
  85.     mapHeight = tileHeight*rows;
  86.     ANI =  tiledLayer.createAnimatedTile(STR);
  87.     int[] map = {//地图数组
  88.        AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
  89.        AIR,ANI,AIR,AIR,AIR, AIR,AIR,AIR,STR,AIR,
  90.        AIR,AIR,STR,AIR,AIR, ANI,AIR,AIR,STR,AIR,
  91.        AIR,AIR,AIR,MON,STR, AIR,AIR,STR,AIR,AIR,
  92.        AIR,AIR,STR,STR,AIR, AIR,AIR,AIR,AIR,AIR,
  93.        AIR,AIR,STR,AIR,ANI, AIR,AIR,AIR,ANI,AIR,
  94.        AIR,AIR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,
  95.        AIR,AIR,STR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
  96.        AIR,STR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,
  97.        AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,
  98.     };//显示地图
  99.     for (int i = 0; i < map.length; i++) {
  100.       int column = i % columns;
  101.       int row = (i - column) / columns;
  102.       tiledLayer.setCell(column, row, map[i]);//通过一个循环来设置tiledLayer
  103.     }
  104.     //tiledLayer.setAnimatedTile(SHN,STR);
  105.     
  106.    // tiledLayer.setCell(4,5, aniStarIndex);
  107.     
  108.     return tiledLayer;//返回
  109.   }
  110.   
  111.   private void render(Graphics g){
  112.     //mLayerManager.paint(g, 0, 0);
  113.     mBackground.paint(g);
  114.     flushGraphics();
  115.   
  116.   }
  117.   synchronized void stop()
  118.   {
  119.     isRun=false;
  120.   }
  121.   public void commandAction(Command c, Displayable d)
  122.   {
  123.     //if any Exit key is pressed then exit
  124.     if(c==exit)
  125.     {
  126.       midlet.exitMidlet();
  127.     }
  128.   }
  129.  
  130.  
  131. }