二战电影坚韧的刺刀:关于J2ME的图像显示问题

来源:百度文库 编辑:高考问答 时间:2024/04/30 07:49:23
为什么不能够把图像显示出来啊!那个高手帮我看看!res中已经放了图片的(PNG的图片)
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class wind extends MIDlet implements CommandListener {

private Command exitCommand , okcommand , scrcommand;
private TextBox tb , tb1;
DrawPicture draw;

public wind() {
super();
exitCommand = new Command("退出", Command.EXIT, 1);
okcommand = new Command("确定", Command.EXIT, 4);
scrcommand = new Command("返回", Command.SCREEN, 4);
tb = new TextBox("Hello MIDlet", "Hello,World!", 15, 0);
tb1 = new TextBox("Hello MIDlet", "Hello,wind.wu", 15, 0);
tb.addCommand(okcommand);
tb.addCommand(exitCommand);
tb1.addCommand(okcommand);
tb1.addCommand(exitCommand);
tb1.addCommand(scrcommand);
draw = new DrawPicture();
draw.addCommand(exitCommand);
tb.setCommandListener(this);
tb1.setCommandListener(this);
;

}

protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(tb);

}

protected void pauseApp() {
System.out.println("pauseApp");
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
System.out.println("destroyApp");

}

public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
try {
destroyApp(false);
} catch (MIDletStateChangeException exception) {
System.out.println("MIDletStateChangeException");
}
notifyDestroyed();
}
if(c == okcommand)
{

Display.getDisplay(this).setCurrent(draw);

}

}

}

-------------------、

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class DrawPicture extends Canvas {

Image pic;

DrawPicture() {
try {
//装入png图片文件
pic = Image.createImage("/*.png");
} catch (IOException e) {
//需要import java.io.*;
System.out.println("load png resource error " + e.getMessage());
}
}

protected void paint(Graphics g) {

g.drawImage(pic, 0, 50, Graphics.TOP| Graphics.LEFT);

}

}

你说你不能够把图像显示出来?
我用了你的程序在WTK里运行,是可以显示图片的呀!
你用一个简单点的程序再试试.如下:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ImageEx extends MIDlet implements CommandListener
{
private Command cmdExit;
private ImageCanvas canvas;
public ImageEx()
{
cmdExit = new Command("Exit", Command.SCREEN, 2);
canvas = new ImageCanvas();
}
public void startApp()
{
canvas.addCommand(cmdExit);
canvas.setCommandListener(this);
Display.getDisplay(this).setCurrent(canvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable
screen)
{
if (command == cmdExit)
{
notifyDestroyed();
}
}
}

class ImageCanvas extends Canvas
{
public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
try
{
Image image = Image.createImage("/sun.png");
g.drawImage(image, 0, 0, g.TOP|g.LEFT);
}catch(Exception e)
{
System.out.println(e.getMessage()) ;
}
}
}