⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tileexample.java

📁 这是j2me的例子,可供初学者学习,里面有源代码,是我写的
💻 JAVA
字号:
/*
 * TileExample.java
 *
 * Created on 2007年3月29日, 下午5:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.tan.ui.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author USER
 */
public class TileExample extends GameCanvas implements Runnable{
    
    private boolean isPlay;
    private long delay;//to give thread delay;
    private int width;
    private int height;//hold the screen x,y;
    private LayerManager layerManager;
    private TiledLayer tiledBackground;
    /** Creates a new instance of ExampleCanvas */
    public TileExample() throws Exception{
        super(true);
        width = getWidth();
        height = getHeight();
        delay = 20;
        tiledBackground = initBackground();
        layerManager = new LayerManager();
        layerManager.append(tiledBackground);
    }
    
    public void run() {
        Graphics g = getGraphics();
        while(isPlay == true) {
            input();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public void start() {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void stop() {
        isPlay = false;
    }
    
    private void input() {
        
    }
    
    private void drawScreen(Graphics g) {
        g.setColor(0xffff);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(0x0000ff);
//        playerSprite.setPosition(currentX,currentY);
//        playerSprite.paint(g);
//        backgroundSprite.paint(g);
//        g.drawString("X",currentX,currentY,Graphics.TOP|Graphics.LEFT);
//        layerManager.setViewWindow(scnX,scnY,140,140);
        layerManager.paint(g,0,0);
        flushGraphics();
    }

    private TiledLayer initBackground() throws Exception{
        Image tileImage = Image.createImage("/tiles.png");
        TiledLayer tiledLayer = new TiledLayer(10,10,tileImage,32,32);
        int[] map={5,  1,  1,  4,  1,  1,  1,  1,  1,  6,
       5,  1,  3,  1,  1,  3,  1,  1,  1,  6,
       5,  1,  2,  1,  1,  2,  1,  1,  1,  6,
       5,  1,  2,  3,  1,  2,  1,  1,  1,  6,
       5,  1,  4,  2,  1,  2,  1,  1,  1,  6,
       5,  1,  1,  4,  1,  2,  1,  1,  1,  6,
       5,  1,  1,  1,  1,  4,  1,  1,  1,  6,
       5,  1,  1,  1,  1,  1,  1,  1,  1,  6,
       5,  1,  1,  1,  1,  1,  1,  1,  1,  6,
       5,  1,  1,  1,  1,  1,  1,  1,  1,  6};
        
        for(int i =0;i<map.length;i++)
        {
            int column = i%10;
            int row = (i-column)/10;
            tiledLayer.setCell(column,row,map[i]);
        }
        return tiledLayer; 
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -