MyCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- import java.io.IOException;
- public class MyCanvas extends GameCanvas implements Runnable,CommandListener
- {
- private LayerManagerDemo 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 boolean isRun;
- private int curX,curY;//当前viewer坐标
- private int xOffset = 5,yOffset = 5;
- private final int SLEEP=10;
- private Command exit;
- public MyCanvas(LayerManagerDemo midlet)
- {
- super(true);
- this.midlet = midlet;
- canvasHeight=getHeight();
- canvasWidth=getWidth();
- try{
- mBackground = createBackground();//创建背景
- }catch(IOException e){
- System.out.println("create error");
- }
- mLayerManager = new LayerManager();//创建层管理实例
- mLayerManager.append(mBackground);//添加地图
- 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)
- {
- input();
- tick();
- render(g);
- Thread.sleep(SLEEP);
- }
- }
- catch(InterruptedException ie)
- {
- System.out.println(ie.toString());
- }
- }
- private TiledLayer createBackground()throws IOException {
- Image image = Image.createImage("/background.png");//创建背景图片
- TiledLayer tiledLayer = new TiledLayer(5, 5, image, 78, 36);//注意:背景的参数的设置!
- int LEA = 1;//草地lea
- int FLD = 2;//田地filed
- int HSE = 3;//房子house
- int TRE = 4;//树木tree
- int WOD = 5;//树丛wood
- int LAK = 6;//湖泊lake
- tileWidth = 78;
- tileHeight = 36;
- columns = 5;
- rows = 5;
- mapWidth =tileWidth*columns;
- mapHeight = tileHeight*rows;
- int[] map = {//地图数组
- LEA,FLD,TRE,FLD,FLD,
- LEA,LEA,LAK,WOD,LEA,
- HSE,LEA,HSE,WOD,FLD,
- WOD,LAK,HSE,LEA,WOD,
- HSE,LEA,LEA,TRE,LAK,
- };//显示地图
- for (int i = 0; i < map.length; i++) {
- int column = i % columns;
- int row = (i - column) / columns;
- tiledLayer.setCell(column, row, map[i]);//通过一个循环来设置tiledLayer
- }
- return tiledLayer;//返回
- }
- private void input(){
- int keyState = getKeyStates(); //获得键盘状态
- if ((keyState & LEFT_PRESSED) != 0) { //如果左键被按下
- curX = curX - xOffset;
- // if(curX >= 0)curX=0;
- }
- if ((keyState & RIGHT_PRESSED) != 0) { //如果右键被按下
- curX = curX + xOffset;
- // if(curX <= -mapWidth + canvasWidth)curX = -mapWidth + canvasWidth;
- }
- if ((keyState & UP_PRESSED) != 0) { //如果上键被按下
- curY = curY - yOffset;
- //if(curY >= 0)curY=0;
- }
- if ((keyState & DOWN_PRESSED) != 0) { //如果下键被按下
- curY = curY + yOffset;
- //if(curY <= -mapHeight + canvasHeight)curY = -mapHeight + canvasHeight;
- }
- }
- private void tick(){
- //mBackground.setPosition(curX,curY);
- // mLayerManager.setViewWindow(curX,curY,canvasWidth/2,canvasHeight/2);
- }
- private void render(Graphics g){
- mBackground.setPosition(50,50);
- mBackground.paint(g);
- //mLayerManager.paint(g,50, 50);
- 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();
- }
- }
- }