当前位置: 代码迷 >> J2ME >> []怎样在游戏中显示二进制文件的图片
  详细解决方案

[]怎样在游戏中显示二进制文件的图片

热度:693   发布时间:2013-02-25 21:34:45.0
[求助]怎样在游戏中显示二进制文件的图片?
我的程序已经可以编译运行了,不过没有显示预期想要的图片,转换后的二进制文件我也不知道要放在那里?
程序如下:
 public Image loadImage(String binFile) //从binFile文件中载入图片
{
byte[] buffer; //读出的图片放入byte数组中。
long len; //数组长度。
int pos; //偏移位置,与isLose取值有关。
 
try
{
InputStream in = this.getClass().getResourceAsStream(binFile);
if(isLose)
pos = 2; //在bin文件中失败时要显示的图片的起始位置。
else
pos = 0; //在bin文件中通关后要显示的图片的起始位置。
in.skip(pos);
 
len = (in.read() & 0xff) << 24;
len |= (in.read() & 0xff) << 16;
len |= (in.read() & 0xff) << 8;
len |= (in.read() & 0xff);
 
buffer = new byte[(int)len];
in.read(buffer,0,buffer.length);
 
in.close();
in = null;
System.gc();  
}
catch(IOException e)
{
e.printStackTrace();
return null;
}
 
return Image.createImage(buffer,0,buffer.length); //返回读入的图片。
}
 
public void goToAlert(String strAlert,String strTicker)
{
info = new Alert(strAlert,"",loadImage("/binFile.bin"),AlertType.INFO);
info.setTimeout(Alert.FOREVER);
msg = new Ticker(strTicker); //跑马灯显示提示文字。
info.setTicker(msg);
 
cmdReStart = new Command("重玩",Command.BACK,1); //重玩按钮
info.addCommand(cmdExit);
info.addCommand(cmdReStart);
info.setCommandListener(this);
display.setCurrent(info,this);
}

------解决方案--------------------------------------------------------
java.lang.OutOfMemoryError 

内存溢出了,你的图片太大了,指的是宽和高太大了
------解决方案--------------------------------------------------------
这个异常的确如楼上所说,或者是你装入的图片数量太多的原因。
  相关解决方案