package prj14_3;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class PPuzzleMIDlet extends MIDlet {
private PPuzzleCanvas canvas;
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
try{
Image img = Image.createImage("/img.jpg");
canvas = new PPuzzleCanvas(img,this);
}catch(Exception ex){
ex.printStackTrace();
}
dis.setCurrent(canvas);
}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
}
package prj14_3;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
public class PPuzzleCanvas extends Canvas implements CommandListener {
private Command cmdResume = new Command("继续", Command.SCREEN, 1);
private Command cmdClose = new Command("关闭", Command.CANCEL, 1);
// 图像
private Image img;
// 每块宽度和高度
private int edge;
// 初始地图数组
int[][] map = { { 00, 01, 02, 03 },
{ 10, 11, 12, 13 },
{ 20, 21, 22, 23 },
{ 30, 31, 32, 33 } };
public Graphics gra = null;
private MIDlet parent;
public PPuzzleCanvas(Image img, MIDlet parent) {
this.img = img;
this.parent = parent;
edge = img.getWidth() / 4;
this.setCommandListener(this);
this.initMap();
}
void initMap() {
Random rnd = new Random();
int temp, x1, y1, x2, y2;
// 将地图数组打乱
for (int i = 0; i < 100; i++) {
x1 = rnd.nextInt(4);
x2 = rnd.nextInt(4);
y1 = rnd.nextInt(4);
y2 = rnd.nextInt(4);
temp = map[x1][y1];
map[x1][y1] = map[x2][y2];
map[x2][y2] = temp;
}
this.repaint();
}
public void paint(Graphics g) {
gra = g;
// 用白色填充背景
g.setColor(255, 255, 255);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
// 根据地图数组中的编号选取图片小块画出来
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (map[x][y] != 33) {
// 获取编号的第一位数
int xSegment = map[x][y] / 10;
// 获取编号的第二位数
int ySegment = map[x][y] % 10;
// 获取图片中左上角坐标为(xSegment*edge, ySegment*edge)
// 宽度为edge,高度为edge的小块
// 画到界面上左上角坐标为(x*edge, y*edge)的位置
g.drawRegion(img, xSegment * edge, ySegment * edge, edge,
edge, Sprite.TRANS_NONE, x * edge, y * edge,
Graphics.LEFT | Graphics.TOP);
}
}
}
if (isSuccess()) {
g.setColor(0, 0, 0);
g.drawString("恭喜您,您已经顺利完成!还要继续吗?", this.getWidth() / 2, this
.getHeight() - 60, Graphics.HCENTER | Graphics.TOP);
this.addCommand(cmdResume);
this.addCommand(cmdClose);
}
}
public void commandAction(Command cmd, Displayable dis) {
if (cmd == cmdClose) {
parent.notifyDestroyed();
} else if (cmd == cmdResume) {
this.initMap();
this.removeCommand(cmdResume);
this.removeCommand(cmdClose);
}
}
public boolean isSuccess() {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
int xSegment = map[x][y] / 10;
int ySegment = map[x][y] % 10;
if (xSegment != x || ySegment != y) {