使用MIDP1.0實現TiledLayer的效果,並實現簡單的滾屏

來源:互聯網
上載者:User

       程式實現的代碼如下:

package gamecanvasuse;

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.FullCanvas;

public class MIDP2Canvas extends FullCanvas {
  Image image = null;

  int index = 0;
  //每個單元使用的圖象分塊內容
  int[][] map = {{0,0,1,3,0,0,0},
                 {0,1,4,4,3,0,0},
                 {1,4,4,4,4,3,0},
      {0,0,0,0,-1,-1,-1}
                };
  public MIDP2Canvas() {
    //載入圖片
    try{
      image = Image.createImage("/res/title.png");
    }catch(Exception e){

    }

  }

  protected void paint(Graphics g) {

    for(int i = 0;i < 4;i++){
      for(int j = 0;j < 4;j++){
        switch (map[j][i + index]){
          case 0:
            g.setColor(0xffffff);
            g.fillRect(i * 32,j * 32,32,32);
            g.setColor(0x000000);
            break;
          case 1:
            //繪製圖塊
            g.setClip(i * 32,j * 32,32,32);
            g.drawImage(image,i * 32,j * 32,Graphics.TOP | Graphics.LEFT);
            g.setClip(0,0,this.getWidth(),this.getHeight());
            break;
          case 2:
            //繪製圖塊
            g.setClip(i * 32,j * 32,32,32);
            g.drawImage(image,i * 32 - 32,j * 32,Graphics.TOP | Graphics.LEFT);
            g.setClip(0,0,this.getWidth(),this.getHeight());
            break;
          case 3:
            //繪製圖塊
            g.setClip(i * 32,j * 32,32,32);
            g.drawImage(image,i * 32 - 64,j * 32,Graphics.TOP | Graphics.LEFT);
            g.setClip(0,0,this.getWidth(),this.getHeight());
            break;
          case 4:
            //繪製圖塊
            g.setClip(i * 32,j * 32,32,32);
            g.drawImage(image,i * 32 - 96,j * 32,Graphics.TOP | Graphics.LEFT);
            g.setClip(0,0,this.getWidth(),this.getHeight());
            break;
        }
      }
    }

    g.setColor(0xff0000);
    g.fillRect(32,32,32,32);

    g.setColor(0x000000);
  }

  public void keyPressed(int keyCode){
    index++;
    repaint();
  }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.