/*
* Flygame.java
*
*/
import java.io.DataInputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.*;
//import com.nokia.mid.ui.*;
/**
*
* @author aflylove
* @version
*/
public class Map extends Canvas implements Runnable {
private Image img;
private Image[] spr;
public boolean kaishi=true;
private short mapWidth,mapHeight,col,row;
private int[] mapdata;
private int guankou;
int m,n;
int[][] data;
public Map(int s)
{
this.guankou=s;
new Thread(this).start();
col=24;
row=24;
mapdata=new int[]{0,2,0,3,0,3,0,0,-4,0,-1,0,0,0,2,-1,2,-1,2,3,5,1,1,5,1,5,4,1,2,2,2,2,0,0,0,3,0,0,-4,0,0,0,0,0,2,-1,2,-1,2,3,5,1,1,5,1,-5,4,1,2,2};
mapWidth=6;
mapHeight=10;
createImg();//图片生成
data=new int[10][6];
for(int i=0;i<10;i++){
for(int j=0;j<6;j++){
data[i][j]=mapdata[i*6+j];
}}
}
public void setMN(int m,int n){
this.m=m;
this.n=n; }
protected void createImg()
{
try{
img= Image.createImage("/map.png");
}catch(java.io.IOException ex){}
int a=img.getWidth()/col;
int b=img.getHeight()/row;
spr=new Image[a*b];
Image pic= Image.createImage(col,row);
Graphics d=pic.getGraphics();
for(int i=0;i<a*b;i++){
d.drawImage(img,-i%a*col,-i/a*row,0);
spr[i]=Image.createImage(pic);
}
}
/* protected void createMap()
{
//InputStream is = this.getClass.getResourceAsStream("/"+str);
// if(is!=null){
// ByteArrayOutputStream baos= new ByteArrayOutputStream();
DataInputStream is = new DataInputStream(getClass().getResourceAsStream("/map.dat"));
byte[] kuangao=new byte[8];
byte[] zhuanhuan=new byte[2];
try
{
is.readFully(kuangao,0,8);
}
catch(Exception e){}
zhuanhuan[0]=kuangao[0];
zhuanhuan[1]=kuangao[1];
mapWidth=(short)byteToint(zhuanhuan);
zhuanhuan[0]=kuangao[2];
zhuanhuan[1]=kuangao[3];
mapHeight=(short)byteToint(zhuanhuan);
zhuanhuan[0]=kuangao[4];
zhuanhuan[1]=kuangao[5];
col=(short)byteToint(zhuanhuan);
zhuanhuan[0]=kuangao[6];
zhuanhuan[1]=kuangao[7];
row=(short)byteToint(zhuanhuan);
mapdata =new byte[mapWidth*mapHeight];
int jishu=0;
try{
while(is.available()>0)
{
mapdata[jishu++] = is.readByte();
}
}catch(Exception e){}
} */
protected void paint(Graphics g)
{
g.setColor(0x00ffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0x00000000);
Image ditu=Image.createImage(mapWidth*col,mapHeight*row);
Graphics h=ditu.getGraphics();
for(int i=0;i<mapdata.length;i++){
if(mapdata[i]<0) continue;
else h.drawImage(spr[mapdata[i]],i%mapWidth*col,i/mapWidth*row,0);
}
Image quantu=Image.createImage(ditu);
g.drawImage(quantu,m,n,0);
}
protected int byteToint(byte[] i)
{
int res;
res = i[1]<<8;
res = res | i[0];
return res;
}
public void run() {
while(kaishi) {
try {
repaint();
Thread.sleep(100);
}catch(InterruptedException e) {}
}
}
/**
* Called when a key is pressed.
*/
protected synchronized void keyPressed(int keyCode) {
}
protected synchronized void keyReleased(int keyCode) {
}
}