首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

郭克华 拼图游戏源码不能显示源图片!解决方法

2012-02-19 
郭克华 拼图游戏源码不能显示源图片!!package prj14_3import javax.microedition.lcdui.Displayimport j

郭克华 拼图游戏源码不能显示源图片!!
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) {


return false;
}
}
}
return true;
}

protected void keyPressed(int keyCode) {
int action = this.getGameAction(keyCode);
int xOf33 = -1, yOf33 = -1;
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (map[x][y] == 33) {
xOf33 = x;
yOf33 = y;
break;
}
}
}
switch (action) {
case GameCanvas.UP:
if (yOf33 != 3) {
this.swap(xOf33, yOf33, xOf33, yOf33 + 1);
}
break;
case GameCanvas.DOWN:
if (yOf33 != 0) {
this.swap(xOf33, yOf33, xOf33, yOf33 - 1);
}
break;
case GameCanvas.LEFT:
if (xOf33 != 3) {
this.swap(xOf33, yOf33, xOf33 + 1, yOf33);
}
break;
case GameCanvas.RIGHT:
if (xOf33 != 0) {
this.swap(xOf33, yOf33, xOf33 - 1, yOf33);
}
break;
}
this.repaint();
}

// 将map中的33和周围的元素对调
public void swap(int xOf33, int yOf33, int targetX, int targetY) {
int temp = map[targetX][targetY];
map[targetX][targetY] = 33;
map[xOf33][yOf33] = temp;
}

// 提示键按下
protected void keyRepeated(int keyCode) {
int action = this.getGameAction(keyCode);
if (action == GameCanvas.FIRE) {
gra.drawImage(img, 0, 0, Graphics.TOP|Graphics.LEFT);
System.out.println("123");
}
}

// 提示键释放
protected void keyReleased(int keyCode) {
int action = this.getGameAction(keyCode);
if (action == GameCanvas.FIRE) {
this.repaint();
}
}
}
我在提示按下那自己加了个System.out.println("123");运行,123能打印,不知道为什么原图片不画出来

[解决办法]
最好 修改成png 格式 ,这是 标准的 小型设备通用 的图片格式。
另外 ,你这里 有 package prj14_3; 请确 定 当前 的 图片img.jpg 直接存储 在 该 目录下 ?

如果 没有 package prj14_3; 那么应该 是可以 存在 res 下的。应该可以通过 编译,并且显示出来。
还有 图片 不能超过屏幕当前 宽高度 ,否则 是 空白 的显示,你 注意下 以上 的问题 ,应该 可以 解决了。

热点排行