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;
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;
}
}
思想很簡略直接,應用圖象的分塊以及根據人物的移動來實現圖片區域顯示