我在Canvas里为何不能drawImage????

joerico 2003-04-07 02:08:59
看我的原代码。
不能显示。

image.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class image extends MIDlet implements CommandListener {

Display display;
private MyCanvas canvas; //创建MyCanvas对象

private Command exitCommand = new Command("EXIT",Command.EXIT,0);

public image() {

display = Display.getDisplay(this);
canvas = new MyCanvas(this); //生成画布
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);

}

public void startApp () {
display.setCurrent(canvas); //显示画布

}

public void destroyApp(boolean unconditional) {
}

public void pauseApp() {
}

public void commandAction(Command c, Displayable s) {
if(c == exitCommand)
destroyApp(true);
notifyDestroyed();
}
}

MyCanvas.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MyCanvas extends Canvas{
private MIDlet midlet;
Image m_face;
int m_maxX = getWidth() - 1;
int m_maxY = getHeight() - 1;
int m_prevX = m_maxX/2;
int m_prevY = m_maxY/2;

public MyCanvas(MIDlet midlet){
this.midlet = midlet;
try{
m_face = Image.createImage("test.png");
}catch(Exception e){
m_face = null;
}
}

public void paint(Graphics g){
g.setColor(0xffffff);
g.fillRect(m_prevX - m_face.getWidth()/2,
m_prevY - m_face.getHeight()/2,
m_face.getWidth(),
m_face.getHeight());
g.setColor(0);

//画新图象
g.drawImage(m_face,0,0,Graphics.VCENTER | Graphics.HCENTER);
}
}

极盼高手赐教。谢谢!!!
...全文
109 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pl5th2001 2003-05-06
哦,要png1.0版本的
回复
joerico 2003-04-07
有一点错误,我改过来了。不是HelloWorld can not create!
现在程序可以运行,可是还是什么都不显示。

我把一个自己做的test.png放到res里,不显示。
回复
joerico 2003-04-07
谢谢!可是还是不行!

错误提示:
HelloWorld can not create!
回复
villagehead 2003-04-07
给你一段代码,你试试能不能画出来!
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

class Navigate2Canvas extends Canvas{

int width=0;
int height=0;

private Image image;
private int newX=0,
newY=0,
stepX=0,
stepY=0;

public Navigate2Canvas(Image image){

this.image=image;
newX=0;
newY=0;
stepX=getWidth()/4;
stepY=getHeight()/4;
}

public void steppingXY(int x,int y){
newX+=x;
newY+=y;
}

public void paint(Graphics g){
width=this.getWidth();
height=this.getHeight();

g.setGrayScale(0);
g.fillRect(0,0,width,height);
g.setGrayScale(255);
g.fillRect(0,0,width,height);

g.translate(newX,newY);
g.drawImage(image,0,0,g.TOP|g.LEFT);
}

protected void keyPressed(int keyCode){
int gameAction=getGameAction(keyCode);
switch(gameAction){
case UP:
steppingXY(0,stepY);
break;
case DOWN:
steppingXY(0,-stepY);
break;
case LEFT:
steppingXY(stepX,0);
break;
case RIGHT:
steppingXY(-stepX,0);
break;
}
repaint();
}

}

public class Navigate2MIDlet extends MIDlet implements CommandListener{

private Command exitCommand;
private Display display;
Displayable nd;

Image image=null;

public Navigate2MIDlet(){
try{
image=Image.createImage("/test.png");
}catch(Exception e){}

Display display=Display.getDisplay(this);

nd=new Navigate2Canvas(image);

exitCommand=new Command("exit",Command.EXIT,1);

nd.addCommand(exitCommand);
nd.setCommandListener(this);
display.setCurrent(nd);

}
public void startApp(){}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c,Displayable s){
notifyDestroyed();
}

}

good luck
回复
joerico 2003-04-07
还是不行,报错是:
com.mot.j2me.midlets.shell.Shell could not be created
javax/microedition/midlet/AppContext::run@71
回复
villagehead 2003-04-07
m_face = Image.createImage("test.png");
换成
m_face = Image.createImage("/test.png");

然后test.png要放到res目录中

再试试吧!

good luck
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2003-04-07 02:08
社区公告
暂无公告