关于J2ME显示图片的问题!急啊!

我之前有C++基础,现在刚开始学J2ME。写了一个小程序,显示一个png格式的图片,遇到了一点问题!代码如下:importjava.io.IOException;impo... 我之前有C++基础,现在刚开始学J2ME。写了一个小程序,显示一个png格式的图片,遇到了一点问题!代码如下:

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class TestImage extends Canvas {

Image img;

public TestImage() throws IOException {
try
{
img = Image.createImage("/res/plane.PNG");
}
catch (IOException e)
{
System.out.println("读取图片异常!"+e.getMessage());
}
}

protected void paint(Graphics g) {

try
{
g.drawImage(img, 50, 50, Graphics.LEFT);
}
catch (Exception e)
{
System.out.println("绘制图片异常!"+e.getMessage());
}
}
}

import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TestImageMain extends MIDlet {

TestImage ti = new TestImage();

public TestImageMain() throws IOException {
Display.getDisplay(this).setCurrent(ti);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

}

已经在res文件夹里面导入了plane.png这个图片,项目根目录里面也有plane.png,可是不知道为什么运行后控制台还是出错:
Running with storage root C:\Documents and Settings\Administrator\j2mewtk\2.5.2\appdb\MediaControlSkin
Running with locale: Chinese_People's Republic of China.936
Running in the identified_third_party security domain
读取图片异常!null
绘制图片异常!
Execution completed.
3403941 bytecodes executed
141 thread switches
1668 classes in the system (including system classes)
17799 dynamic objects allocated (532080 bytes)
2 garbage collections (458244 bytes collected)

我把img = Image.createImage("/res/plane.PNG");这一句改成
img = Image.createImage("/plane.PNG");之后,错误信息变成了:

Running with storage root C:\Documents and Settings\Administrator\j2mewtk\2.5.2\appdb\MediaControlSkin
Running with locale: Chinese_People's Republic of China.936
Running in the identified_third_party security domain
绘制图片异常!
Execution completed.
3404368 bytecodes executed
213 thread switches
1668 classes in the system (including system classes)
17786 dynamic objects allocated (538996 bytes)
2 garbage collections (458244 bytes collected)

请各位大神帮帮忙,小弟新手,感激不尽!
展开
 我来答
wngzhng11
2012-02-08 · 超过16用户采纳过TA的回答
知道答主
回答量:96
采纳率:0%
帮助的人:36.5万
展开全部
1. 写图片位置的时候,默认的就是res,所以不能再加res/ 了
2. g.drawImage(img, 50, 50, Graphics.LEFT);
这句不对,括号里的第4个参数是锚点,需要由 上下 和 左右 2部分构成, 写成0的话就是默认左上,换算下来就是 Graphics.LEFT|Graphics.TOP,就是需要2个方向,中间加上“|”的格式,来确认锚点位置,你只写了一个方向,就会出错。
追问
恩,试试。谢谢
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式