Posted on 2007-04-28 16:27
笨蛋啊帆 阅读(546)
评论(1) 编辑 收藏
import javax.microedition.lcdui.*;
import java.io.InputStream;
/** index
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DrawMap extends Canvas implements CommandListener {
public int nMapCell[][]; //地图数据列表
public int nMapTans[][]; //地图翻转数据
public int nTileWidth;
public int nTileHeight;
public int nMapWidth;
public int nMapHeight;
public int bufWidth ;
public int bufHeight ;
public int MapX = 0;
public int bufPosX = 0;
public int OldbufPosX = 0;
public int DrawPosX;
public int ncount=1;
int bufi,bufj;
int Maxj;
boolean isOverMap = false;
Graphics bufGraphics;
int ScrW =getWidth();
int ScrH =getHeight();
int i, j;
int index,Windex,Hindex ;
Image m_image;
Image bufimage;
public DrawMap() {
try {
m_image = Image.createImage("/1.png");
InputStream MapData = getClass().getResourceAsStream("/Map.dat");
nMapWidth = MapData.read();
MapData.read();
nMapHeight = MapData.read();
MapData.read();
nTileWidth = MapData.read();
MapData.read();
nTileHeight = MapData.read();
MapData.read();
nMapCell = new int[nMapHeight][nMapWidth];
nMapTans = new int[nMapHeight][nMapWidth];
for(i=0; i<nMapHeight; i++)
{
for(j=0; j<nMapWidth; j++)
{
nMapCell[j] = MapData.read();
nMapTans[j] = MapData.read();
}
}
bufWidth = (((ScrW + nTileWidth) / nTileWidth + 2) * nTileWidth) ;
bufHeight =(((ScrH + nTileHeight) / nTileHeight + 2) * nTileHeight) ;
bufHeight = bufHeight > nMapHeight ? nMapHeight * nTileHeight : bufHeight;
bufimage = Image.createImage(bufWidth,bufHeight);
bufGraphics = bufimage.getGraphics();
//bufGraphics.drawImage(m_image,0,0,0);
MapX = bufWidth ;
for (i = 0; i < bufHeight / nTileHeight ; i++)
{
for (j = 0 ; j <bufWidth / nTileWidth ; j++ )
{
index = nMapCell[j];
Hindex =index / (m_image.getWidth()/nTileHeight);
Windex =index % (m_image.getWidth()/nTileWidth);
bufGraphics.drawRegion(m_image, Windex * nTileWidth,
Hindex * nTileHeight, nTileWidth,
nTileHeight, 0, j * nTileWidth,
i * nTileHeight, 20);
}
}
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
// Set up this Displayable to listen to command events
setCommandListener(this);
// add the Exit command
addCommand(new Command("Exit", Command.EXIT, 1));
}
public void commandAction(Command command, Displayable displayable) {
/** @todo Add command handling code */
if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
show.quitApp();
}
}
protected void keyPressed(int keyCode){
switch (keyCode)
{
case KEY_NUM4 :
bufPosX +=3;
break;
case KEY_NUM6 :
// bufPosX -=3;
OverMoved(-29,bufGraphics);
break;
}
repaint();
}
/*****
* 只实现了向右滚动。
*/
public void OverMoved(int MoveX,Graphics g){
OldbufPosX = bufPosX;
bufPosX = bufPosX + MoveX;
System.out.println(nMapWidth*nTileWidth);
Image tmpImage;
if (MoveX < 0 && (bufPosX > 0 ? bufPosX : -bufPosX) / nTileWidth >= 1) {
if (bufPosX > OldbufPosX) {
Maxj = bufWidth / nTileWidth;
//MapX = MapX + bufWidth;
//Maxj = (bufPosX > 0 ? bufPosX : -bufPosX) / nTileWidth;
//System.out.println(Maxj);
} else {
Maxj = (bufPosX > 0 ? bufPosX : -bufPosX) / nTileWidth;
}
//Maxj = (bufPosX > 0 ? bufPosX : -bufPosX) / nTileWidth;
//MapX = bufWidth * ncount / nTileWidth;
for (i = 0; i < bufHeight / nTileHeight; i++) {
for (j = (OldbufPosX > 0 ? OldbufPosX : -OldbufPosX) / nTileWidth;j < Maxj; j++) {
index = nMapCell[(MapX / nTileWidth + j ) % nMapWidth];
//System.out.println("index="+index);
Hindex = index / (m_image.getWidth() / nTileWidth);
Windex = index % (m_image.getWidth() / nTileWidth);
//System.out.println(j);
tmpImage = Image.createImage(m_image, Windex * nTileWidth,
Hindex * nTileHeight, nTileWidth,
nTileHeight, 0);
g.drawImage(tmpImage, j * nTileWidth, i * nTileHeight, 0);
}
}
if ((bufPosX > 0 ? bufPosX : -bufPosX) >= bufWidth) {
bufPosX = 0;
//OldbufPosX = 0;
MapX = (MapX + bufWidth)%(nMapWidth*nTileWidth);
//MapX = MapX + bufWidth;
}
DrawPosX = bufPosX + bufWidth;
}
repaint();
}
/******
* 有问题的算法。
*/
/*
public void OverMoved(int MoveX,Graphics g){
OldbufPosX = bufPosX;
bufPosX = bufPosX + MoveX;
Image tmpImage;
if ( MoveX < 0 &&(bufPosX > 0 ? bufPosX : -bufPosX)/ nTileWidth >=1 ){
DrawPosX = bufPosX + bufWidth;
if (bufPosX > OldbufPosX){
Maxj = bufWidth / nTileWidth;
}else{
Maxj = (bufPosX > 0 ? bufPosX : -bufPosX) / nTileWidth;
}
MapX = bufWidth * ncount / nTileWidth;
if ( (bufPosX > 0 ? bufPosX : -bufPosX) >= bufWidth ){
bufPosX = 0;
//OldbufPosX = 0;
if ((ncount + 2) * bufWidth > nMapWidth * nTileWidth) {
isOverMap = true;
}
ncount ++ ;
//ncount = (++ncount) %(nMapWidth/(bufWidth/nTileWidth));
}
for (i = 0 ; i < bufHeight/nTileHeight; i++)
{
for (j = (OldbufPosX > 0 ? OldbufPosX : -OldbufPosX)/nTileWidth ;j < Maxj ; j++){
index = nMapCell[(MapX+j)%nMapWidth];
//System.out.println("index="+index);
Hindex =index / (m_image.getWidth()/nTileWidth);
Windex =index % (m_image.getWidth()/nTileWidth);
//System.out.println(j);
tmpImage = Image.createImage(m_image,Windex*nTileWidth,Hindex*nTileHeight,nTileWidth,nTileHeight,0);
g.drawImage(tmpImage,j*nTileWidth,i*nTileHeight,0);
}
}
}
repaint();
}
*/
protected void paint(Graphics g) {
/** @todo Add paint codes */
g.setColor(0xffffff);
g.fillRect(0,0,ScrW,ScrH);
g.drawImage(bufimage,bufPosX,0,0);
g.drawImage(bufimage,DrawPosX,0,0);
}
}