地圖右滾 2D卷軸算法

          Posted on 2007-04-28 16:27 笨蛋啊帆 閱讀(550) 評論(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);
              }
          }

          Feedback

          # re: 地圖右滾 2D卷軸算法  回復  更多評論   

          2007-05-24 18:03 by zhangzhong
          人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意人同意

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          posts - 51, comments - 17, trackbacks - 0, articles - 0

          Copyright © 笨蛋啊帆

          主站蜘蛛池模板: 九龙城区| 股票| 尉犁县| 会宁县| 盐城市| 富顺县| 安宁市| 黄陵县| 泰顺县| 苏尼特右旗| 定结县| 梁山县| 遂平县| 绥中县| 阳原县| 梁河县| 平塘县| 阿巴嘎旗| 九江市| 乌鲁木齐县| 阿勒泰市| 驻马店市| 香河县| 新野县| 黎城县| 泌阳县| 敦煌市| 突泉县| 新丰县| 临城县| 友谊县| 仙桃市| 那坡县| 宜良县| 九龙坡区| 西和县| 汝城县| 电白县| 化州市| 安庆市| 逊克县|