程序实现的代码如下:
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();
}
}