MyCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import java.io.IOException;
- public class MyCanvas extends GameCanvas implements Runnable,CommandListener
- {
- private StarDemo midlet;//程序控制类
- private volatile Thread animationThread=null;//线程
- private TiledLayer mBackground;//地图
- private LayerManager mLayerManager;
- private int canvasHeight, canvasWidth;//屏幕高度/宽度
- private static int tileHeight,tileWidth;//地图的高度/
- private static int columns,rows;
- private static int mapWidth,mapHeight;
- private int MON = 1;//月亮moon
- private int SHN = 2;//闪烁shine
- private int STR = 3;//星星star
- private int AIR = 4;//夜空air
- private int ANI;//动画贴片animated
- private int aniStarIndex;
- private int aniCounter;
- private boolean isRun;
- private int curX,curY;//当前viewer坐标
- private final int SLEEP=10;
- private Command exit;
- public MyCanvas(StarDemo midlet)
- {
- super(true);
- this.midlet = midlet;
- canvasHeight=getHeight();
- canvasWidth=getWidth();
- System.out.println(canvasWidth);
- System.out.println(canvasHeight);
- try{
- mBackground = createBackground();//创建背景
- }catch(IOException e){
- System.out.println("create error");
- }
- curX = -(mapWidth - canvasWidth)/2;
- curY = -(mapHeight - canvasHeight)/2;
- mBackground.setPosition(curX,curY);
- exit=new Command("Exit", Command.STOP,1);
- this.addCommand(exit);
- this.setCommandListener(this);//两个按键
- }
- synchronized void start()
- {
- isRun=true;
- animationThread=new Thread(this);
- animationThread.start();
- }
- public void run()
- {
- Graphics g = this.getGraphics();
- try
- {
- while (isRun)
- {
- tick();
- render(g);
- Thread.sleep(SLEEP);
- }
- }
- catch(InterruptedException ie)
- {
- System.out.println(ie.toString());
- }
- }
- private void tick(){
- aniCounter++;
- if(aniCounter%6 == 0)
- mBackground.setAnimatedTile(ANI,SHN);
- else
- mBackground.setAnimatedTile(ANI,STR);
- }
- private TiledLayer createBackground()throws IOException {
- Image image = Image.createImage("/star.png");//创建背景图片
- TiledLayer tiledLayer = new TiledLayer(6, 6, image, 32, 32);//注意:背景的参数的设置!
- tileWidth = 32;
- tileHeight = 32;
- columns = 6;
- rows = 6;
- mapWidth =tileWidth*columns;
- mapHeight = tileHeight*rows;
- ANI = tiledLayer.createAnimatedTile(STR);
- int[] map = {//地图数组
- STR,AIR,AIR,ANI,AIR,AIR,
- AIR,MON,STR,AIR,AIR,AIR,
- STR,STR,AIR,AIR,ANI,AIR,
- STR,AIR,ANI,AIR,AIR,AIR,
- AIR,AIR,AIR,STR,AIR,AIR,
- AIR,AIR,AIR,STR,AIR,AIR,
- };//显示地图
- for (int i = 0; i < map.length; i++) {
- int column = i % columns;
- int row = (i - column) / columns;
- tiledLayer.setCell(column, row, map[i]);//通过一个循环来设置tiledLayer
- }
- //tiledLayer.setAnimatedTile(SHN,STR);
- // tiledLayer.setCell(4,5, aniStarIndex);
- return tiledLayer;//返回
- }
- private void render(Graphics g){
- //mLayerManager.paint(g, 0, 0);
- mBackground.paint(g);
- flushGraphics();
- }
- synchronized void stop()
- {
- isRun=false;
- }
- public void commandAction(Command c, Displayable d)
- {
- //if any Exit key is pressed then exit
- if(c==exit)
- {
- midlet.exitMidlet();
- }
- }
- }