import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDletMain extends MIDlet implements Runnable{
private static Display dis;
private static CanvasMain cas;
private static Thread t;
public MIDletMain() {
dis = Display.getDisplay(this);
cas = new CanvasMain();
dis.setCurrent(cas);
}
protected void startApp() throws MIDletStateChangeException {
t = new Thread(this);
t.start();
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
CanvasMain.exit();
notifyDestroyed();
}
public final void run() {
cas.run();
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class CanvasMain extends Canvas{
/** 键值 */
// 通用键值
public static final byte AVK_0 = 48;
public static final byte AVK_1 = 49;
public static final byte AVK_2 = 50;
public static final byte AVK_3 = 51;
public static final byte AVK_4 = 52;
public static final byte AVK_5 = 53;
public static final byte AVK_6 = 54;
public static final byte AVK_7 = 55;
public static final byte AVK_8 = 56;
public static final byte AVK_9 = 57;
public static final byte AVK_STAR = 42;
public static final byte AVK_POUND = 35;
// 标准键值
public static final byte AVK_UP = -1;
public static final byte AVK_DOWN = -2;
public static final byte AVK_LEFT = -3;
public static final byte AVK_RIGHT = -4;
public static final byte AVK_SELECT = -5;
public static final byte AVK_SOFT1 = -6;
public static final byte AVK_SOFT2 = -7;
// Nokia键值
public static final byte AVK_PHONE = -10;
// 索爱键值
public static final byte AVK_BACK = -11;
public static final byte AVK_C = -8;
// 摩托键值
// public static final byte AVK_UP = 1;
// public static final byte AVK_DOWN = 6;
// public static final byte AVK_LEFT = 2;
// public static final byte AVK_RIGHT = 5;
// public static final byte AVK_SELECT = 20;
// public static final byte AVK_SOFT1 = 22;
// public static final byte AVK_SOFT2 = 21;
// public static final byte AVK_C = 10;
/** 资源初始化 */
private static final void init() {
mapInit();
screenInit();
System.gc();
}
/** 构造函数 */
public CanvasMain() {
setFullScreenMode(true);
// 屏幕宽高
screenW = 154;
screenH = 136;
// 滚屏速度
speed = 7;
// 地图单元格大小
mapCellW = 16;
mapCellH = 16;
init();
}
/** 线程 */
private static final int INTERVALTIME = 80;
private static boolean isRun;
// 启动线程
private static final void start() {
isRun = true;
}
// 停止线程
public static final void exit() {
isRun = false;
}
// 线程循环
public final void run() {
start();
long oldTime,intervalTime;
while(isRun) {
oldTime = System.currentTimeMillis();
keyRepeated();
flushGraphics();
intervalTime = INTERVALTIME – System.currentTimeMillis() + oldTime;
if(intervalTime > 0) {
try {
Thread.sleep(intervalTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
exit();
}
/** 绘图 */
public final void paint(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0,0,getWidth(),getHeight());
mapPaint(g);
}
private final void flushGraphics() {
repaint();
serviceRepaints();
}
/** 用户输入 */
public static int keyCode;
protected final void keyPressed(int k) {
keyCode = k;
input();
flushGraphics();
}
protected final void keyRepeated() {
input();
}
protected final void keyReleased(int k) {
keyCode = 0;
input();
flushGraphics();
}
public static final void input(){
switch(keyCode)
{
case AVK_UP:
case AVK_2:
if(screenY <= speed)
screenY = 0;
else
screenY -= speed;
break;
case AVK_DOWN:
case AVK_8:
if(screenY >= mapH – screenH – speed)
screenY = mapH – screenH;
else
screenY += speed;
break;
case AVK_LEFT:
case AVK_4:
if(screenX <= speed)
screenX = 0;
else
screenX -= speed;
break;
case AVK_RIGHT:
case AVK_6:
if(screenX >= mapW – screenW – speed)
screenX = mapW – screenW;
else
screenX += speed;
break;
}
}
/** 地图 */
// 地图单元格的宽高
private static byte mapCellW,mapCellH;
// 地图平铺数组
private static byte[][] map;
// 地图宽高
private static int mapW,mapH;
// 地图列数、行数
private static int mapCol,mapRow;
// 地图单元格的宽高
private static Image[] imgMapCell;
private static final void mapInit(){
map = new byte[][]{
{68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,
68,68,68,68,28,9 ,18,3 ,3 ,73,19,25,25,},
{68,68,68,68,38,38,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,
68,68,70,68,28,18,3 ,3 ,13,14,3 ,3 ,19,},
{68,68,68,28,7 ,8 ,26,68,68,68,69,69,69,69,69,69,68,70,68,68,68,68,38,38,38,38,38,38,68,68,68,28,0 ,
2 ,2 ,4 ,5 ,1 ,26,68,},
{68,68,68,28,16,17,26,68,68,68,69,69,68,68,68,69,68,68,68,68,68,28,0 ,2 ,
2 ,2 ,2 ,1 ,26,68,38,39,9 ,29,29,29,29,10,26,68,},
{68,68,68,68,50,50,68,68,68,68,69,69,68,68,68,69,69,72,69,69,68,28,18,20,29,29,44,
10,26,28,0 ,2 ,11,29,29,30,29,10,26,68,},
{68,70,68,68,68,68,68,68,70,68,69,69,68,68,68,68,68,68,69,69,69,69,72,27,29,29,53,
10,26,28,9 ,29,29,29,29,29,29,10,26,68,},
{68,68,68,68,68,68,68,68,68,68,69,69,68,68,68,68,70,68,68,69,69,69,72,36,29,29,29,
10,26,28,18,3 ,20,29,29,29,29,10,26,68,},
{68,68,68,68,68,68,68,74,68,68,72,69,68,68,68,68,68,68,68,69,69,68,28,18,3 ,13,14,19,26,68,68,62,9 ,29,29,29,29,10,26,68,},
{68,68,68,70,68,68,68,68,68,68,69,69,68,68,38,38,38,68,68,69,69,68,68,62,29,29,23,
77,68,68,68,28,18,3 ,13,14,3 ,19,37,38,},
{68,68,74,68,68,68,68,68,68,68,69,69,70,28,29,44,30,26,68,69,69,68,68,68,50,72,
72,68,68,68,68,68,50,62,29,46,77,62,7 ,8 ,},
{68,68,68,68,68,68,68,70,68,68,69,69,68,28,24,53,35,72,69,69,69,68,68,68,68,69,
69,68,68,68,68,68,68,28,29,46,26,28,16,17,},
{68,68,68,68,68,68,68,68,68,68,69,69,68,28,29,29,24,26,68,69,69,68,68,68,68,69,
69,68,74,68,68,68,68,68,62,46,26,68,50,50,},
{68,68,68,68,68,74,68,68,68,68,72,72,68,68,50,72,50,68,68,69,69,68,68,68,68,69,
68,68,68,68,68,68,68,68,28,46,26,68,68,70,},
{38,38,38,38,68,68,68,68,38,39,29,29,37,68,68,69,68,68,68,69,69,68,68,68,68,69,
68,68,68,68,68,68,68,68,28,46,37,38,68,68,},
{29,29,29,29,71,71,71,71,29,29,29,29,35,72,69,69,69,69,69,69,69,68,68,69,69,69,
68,68,68,68,68,68,68,68,28,48,52,47,26,68,},
{29,29,29,29,71,71,71,71,29,29,29,29,24,72,69,69,69,69,69,69,72,68,68,69,69,69,
68,68,70,68,68,68,68,68,68,50,62,46,37,38,},
{50,50,50,50,68,68,68,62,29,29,29,29,24,26,68,68,68,68,68,68,68,68,68,69,69,69,
68,68,68,68,68,68,70,68,68,68,28,48,52,47,},
{68,68,68,68,68,68,68,68,50,50,50,50,50,68,68,68,68,68,68,68,68,68,68,69,69,68,
68,68,68,68,68,68,68,68,68,68,68,50,62,46,},
{38,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,74,68,68,70,68,68,68,69,69,68,
68,68,68,68,68,68,74,68,68,68,68,68,28,46,},
{44,37,68,68,68,68,68,70,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,69,69,69,
69,69,69,69,69,69,68,68,68,68,68,68,28,46,},
{53,29,37,68,68,68,74,68,68,38,38,38,38,38,68,68,68,68,68,68,68,68,68,69,69,69,
69,69,69,69,72,72,38,68,68,68,68,68,28,46,},
{25,29,24,37,68,68,68,68,28,0 ,2 ,2 ,2 ,1 ,26,68,68,68,70,68,74,68,68,68,68,68,68,28,0 ,2 ,4 ,5 ,1 ,26,68,68,68,68,28,46,},
{25,29,29,29,71,71,71,71,71,27,29,29,29,10,26,68,68,68,68,68,68,68,68,70,68,68,
38,39,9 ,29,29,29,10,26,68,70,68,68,28,46,},
{25,24,29,29,71,71,71,71,71,36,29,29,29,10,26,68,68,68,68,68,68,68,68,70,68,28,
24,29,6 ,29,29,44,10,26,68,68,68,68,28,46,},
{25,29,29,77,68,28,0 ,2 ,2 ,18,3 ,13,14,19,37,38,38,38,38,38,38,38,68,68,68,28,29,29,15,29,29,53,10,26,68,68,68,
68,28,46,},
{29,29,29,26,68,28,9 ,23,29,29,29,29,29,0 ,2 ,2 ,2 ,2 ,1 ,29,29,24,26,68,68,28,29,29,18,3 ,3 ,3 ,19,26,68,68,68,68,28,46,},
{29,72,77,68,68,28,9 ,29,0 ,2 ,2 ,2 ,2 ,11,29,29,29,44,10,7 ,8 ,29,26,68,68,28,29,29,29,29,29,29,29,26,68,68,68,68,28,46,},
{72,72,68,68,68,28,9 ,29,9 ,29,29,29,29,29,35,29,29,53,10,16,17,29,37,38,68,68,50,50,50,50,72,72,50,68,68,
68,68,70,28,46,},
{69,69,68,70,68,28,9 ,29,18,13,14,3 ,3 ,20,29,29,29,29,10,2 ,2 ,2 ,2 ,1 ,37,68,68,68,68,68,69,69,68,68,68,68,68,38,39,46,},
{69,69,68,74,68,28,9 ,29,35,29,29,0 ,2 ,9 ,29,29,29,29,6 ,29,29,24,29,6 ,35,26,68,68,68,68,69,69,68,68,74,68,72,52,52,45,},
{72,72,68,68,68,28,9 ,29,23,29,29,9 ,29,9 ,29,29,24,29,15,35,29,29,29,15,24,72,69,69,68,68,69,69,68,68,68,68,69,50,62,29,},
{29,29,26,68,68,28,18,3 ,20,29,29,9 ,29,18,3 ,3 ,3 ,3 ,19,29,29,29,29,10,29,26,68,69,69,68,69,69,68,68,68,68,69,68,68,50,},
{29,25,26,68,68,68,50,62,9 ,29,29,9 ,29,29,29,29,25,25,29,21,3 ,20,35,10,29,26,68,69,69,69,69,69,68,68,68,68,69,68,68,68,},
{29,29,37,38,38,38,70,28,18,13,14,18,3 ,3 ,3 ,3 ,3 ,3 ,3 ,19,29,18,3 ,19,29,26,68,68,68,68,68,68,68,68,68,68,69,68,68,68,},
{25,29,29,29,29,29,37,68,50,62,29,29,29,29,29,24,29,29,29,29,29,29,25,25,77,68,
68,70,68,68,68,68,68,68,68,68,69,69,69,68,},
{25,29,29,29,29,25,29,26,68,68,50,50,50,50,50,50,50,50,50,50,50,50,50,50,68,74,
68,68,68,68,68,70,68,68,68,68,68,68,69,68,},
{44,29,29,29,29,29,29,37,38,38,68,68,74,68,68,70,68,68,68,38,38,38,38,38,38,38,
68,69,69,69,72,69,69,69,68,68,68,68,69,68,},
{53,29,29,29,29,29,29,25,29,29,26,68,68,68,68,68,68,68,28,29,0 ,2 ,2 ,2 ,
2 ,1 ,26,69,68,68,68,68,68,69,68,68,38,38,72,38,},
{25,29,29,29,29,29,29,29,24,24,72,69,69,69,69,69,68,68,28,29,27,29,29,29,29,6 ,26,69,68,68,74,68,68,69,68,28,29,30,29,29,},
{25,25,25,25,25,29,29,29,29,77,68,68,68,68,68,69,69,69,72,29,36,29,29,25,29,15,
72,69,68,68,68,68,68,69,69,72,29,29,29,35,},
};
mapRow = map.length;
mapCol = map[0].length;
mapW = mapCellW * mapCol;
mapH = mapCellH * mapRow;
imgMapCell = cellImage("/village.png",mapCellW,mapCellH);
}
private static final void mapPaint(Graphics g){
// 计算地图平铺的起始行列、终止行列数
int startCol = screenX/mapCellW;
if(startCol < 0)
startCol = 0;
int endCol = startCol + screenCol;
if((screenX % mapCellW) > screenHDepart)
endCol ++;
if(endCol > mapCol )
endCol = mapCol ;
int startRow = screenY/mapCellH;
if(startRow < 0)
startRow = 0;
int endRow = startRow + screenRow;
if((screenY % mapCellH) > screenVDepart)
endRow ++;
if(endRow > mapRow)
endRow = mapRow;
// 铺地图
g.setColor(0x00FF00);
for(int i=startCol;i<endCol;i++){
for(int j=startRow;j<endRow;j++){
byte tiled = map[j][i];
if(tiled != -1){
g.drawImage(imgMapCell[tiled],32+mapCellW * i – screenX,64+mapCellH * j – screenY,0);
g.drawRect(32+mapCellW * i – screenX,64+mapCellH * j – screenY,mapCellW,mapCellH);
}
}
}
g.setColor(0x000000);
g.drawRect(32,64,screenW,screenH);
g.drawRect(33,65,screenW-2,screenH-2);
g.drawString("屏幕大小"+screenW+"*"+screenH,5,5,0);
g.drawString("移动速度"+speed,120,5,0);
g.drawString("黑框区域表示屏幕范围",5,30,0);
}
/** 屏幕 */
private static int screenX,screenY,screenW,screenH;
private static int screenCol,screenRow;
private static byte screenHDepart,screenVDepart;
private static byte speed;
private static final void screenInit(){
// 计算屏幕内的行列数
screenCol = screenW / mapCellW;
if(screenW % mapCellW != 0)
screenCol ++;
if(screenCol > mapCol)
screenCol = mapCol;
screenRow = screenH / mapCellH;
if(screenH % mapCellH != 0)
screenRow ++;
if(screenRow > mapRow)
screenRow = mapRow;
// 计算屏幕大小与单元格的偏差值
screenHDepart = (byte) (mapCellW * screenCol – screenW);
screenVDepart = (byte) (mapCellH * screenRow – screenH);
}
/** 工具函数 */
// 创建图片
public static final Image createImage(String name){
try {
return Image.createImage(name);
} catch (IOException e) {
System.out.println("createImage");
}
return null;
}
// 从指定图片中获得固定单元格大小的切割图片数组
public static final Image[] cellImage(String name,int cellW,int cellH){
Image img = createImage(name);
if(img == null){
System.out.println("cellImage");
return null;
}
int t_ImgW = img.getWidth();
int t_ImgH = img.getHeight();
if(t_ImgW % cellW != 0 || t_ImgH % cellH != 0){
System.out.println("cellImage");
return null;
}
int row = t_ImgH / cellH;
int col = t_ImgW / cellW;
Image[] o_Img = new Image[row*col];
for(int i=0;i<o_Img.length;i++)
o_Img[i] = Image.createImage(img,cellW*(i%col),cellH*(i/col),cellW,cellH,0);
img = null;
System.gc();
return o_Img;
}
}
思想很简单直接,使用图象的分块以及根据人物的移动来实现图片区域显示