关于repaint()方法调用问题

chenzujie 2010-09-27 11:57:33
小弟我自己新建一个Canvas类:
public class DemoCanvas extends Canvas
{
private String textShow="主界面";
public DemoCanvas()
{
this.setTitle(textShow);
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0,getWidth(),getHeight());
g.drawImage(imageBook[page], 0, 0, Graphics.TOP|Graphics.LEFT);
}
protected void keyPressed(int keyCode){
if(getGameAction(keyCode)==RIGHT){
if(page==0)

page++;
if(page<14)
this.setTitle("小猪发脾气");
repaint();
}
}
想要在主Midlet类中
else if(Command.OK==c.getCommandType())
{
int i=listmenu.getSelectedIndex();
page=i;
myCanvas.repaint();//myCanvas是DemoCanvas型变量
}调用repaint(),但却不会显示调用,我想知道为什么啊?
...全文
278 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenzujie 2010-09-28
其实那个list是用来做选择进入的页面,相当于那个list是个目录,选择进入哪个故事界面。[Quote=引用 8 楼 colaice2009 的回复:]
不清楚lz要什么效果。你在List里调用Canvas的重绘事件怎么能看到呢。
另回4楼,重绘不需实现Runnable。
[/Quote]
回复
colaice2009 2010-09-28
不清楚lz要什么效果。你在List里调用Canvas的重绘事件怎么能看到呢。
另回4楼,重绘不需实现Runnable。
回复
zhhaogen 2010-09-28
为什么if(Command.OK==c.getCommandType())?
貌似应该是if(c.getCommandType()=Command.OK)
回复
chenzujie 2010-09-28
不是的:代码如下:
public class Midlet1 extends MIDlet implements CommandListener{

private Image[] imageBook;
private DemoCanvas myCanvas;
private Command exitCommand,back;
private Command menuList,select;
private Command backmenu;
private String[] StringArray={"小猪发脾气","真假小白兔","小鼹鼠请客"};
private List listmenu;
private int page=0;
public Midlet1() throws IOException {
// TODO Auto-generated constructor stub
imageBook=new Image[40];
imageBook[0]=Image.createImage("/ima.png");
imageBook[1]=Image.createImage("/f1.png");
imageBook[2]=Image.createImage("/f2.png");
imageBook[3]=Image.createImage("/f3.png");
imageBook[4]=Image.createImage("/f4.png");
imageBook[5]=Image.createImage("/f5.png");
imageBook[6]=Image.createImage("/f6.png");
imageBook[7]=Image.createImage("/f7.png");
imageBook[8]=Image.createImage("/f8.png");
imageBook[9]=Image.createImage("/f9.png");
imageBook[10]=Image.createImage("/f10.png");
imageBook[11]=Image.createImage("/f11.png");
imageBook[12]=Image.createImage("/f12.png");
imageBook[13]=Image.createImage("/f13.png");
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
myCanvas=new DemoCanvas();
Command exitCommand=new Command("退出",Command.EXIT,2);
Command menuList=new Command("目录列表",Command.SCREEN,1);
Command back=new Command("Back",Command.EXIT,1);
Command select=new Command("选择",Command.OK,1);
Command backmenu=new Command("返回目录列表",Command.BACK,1);
myCanvas.addCommand(exitCommand);//载入退出命令
myCanvas.addCommand(menuList);//载入进入目录列表的命令
myCanvas.addCommand(back);
myCanvas.setCommandListener(this);
listmenu=new List("目录菜单",List.EXCLUSIVE,StringArray,null);
listmenu.addCommand(exitCommand);
listmenu.addCommand(select);
listmenu.addCommand(back);
listmenu.setSelectCommand(select);
listmenu.setCommandListener(this);
listmenu.setFont(0, Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_LARGE));
listmenu.setFont(1, Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_LARGE));
listmenu.setFont(2, Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_LARGE));
Display.getDisplay(this).setCurrent(myCanvas);

}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c.getCommandType()==Command.EXIT)
{
try {
destroyApp(false);

} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}
else if(Command.SCREEN==c.getCommandType())
{
Display.getDisplay(this).setCurrent(listmenu);
}
else if(Command.OK==c.getCommandType())
{
int i=listmenu.getSelectedIndex();
page=i+1;
myCanvas.repaint();
}
}[Quote=引用 6 楼 prince58 的回复:]
这个变量 myCanvas 是 static 的吗? 可以把Midlet类的代码帖出来看一下。
[/Quote]
回复
prince58 2010-09-28
这个变量 myCanvas 是 static 的吗? 可以把Midlet类的代码帖出来看一下。
回复
prince58 2010-09-28
不建意在 keyPressed 里调用repaint, 这个keyPressed有时可能会不响应,或是有延时(真机上)。这样会照成画面更新不及时。
回复
prince58 2010-09-28
奇怪,记得昨天回复过类似的帖的,找不到了。

这个很简单,你还要实现Runnable接口,并在run()的循环里调用repaint(),不然paint()只有在初始化时被调用 一次。

说细的看这篇:
http://blog.csdn.net/prince58/archive/2010/05/18/5603224.aspx
回复
凡员外 2010-09-28
repaint方法好像是protected的吧
回复
凡员外 2010-09-28
没看出来
回复
QQ153984069 2010-09-28
package prj1_1_1;

import java.io.IOException;

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

public class DemoCanvas extends Canvas {
private String textShow = "主界面";
private Image img1=createImage("/dinosaur.png"),img2=createImage("/leaf.jpg");
private Image[] imageBook={img1,img2};
private int page=0;

public DemoCanvas() {
this.setTitle(textShow);

}

public DemoCanvas(int page) {
this.page=page;
this.setTitle(textShow);

}

public void paint(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(imageBook[page], 0, 0, Graphics.TOP | Graphics.LEFT);
}

protected void keyPressed(int keyCode) {
System.out.println("getGameAction(keyCode):"+getGameAction(keyCode));
if (getGameAction(keyCode) == RIGHT) {
if (page == 0)
page++;
if (page < 2)
this.setTitle("小猪发脾气");
repaint();
}
}
private Image createImage(String str){

try {
return Image.createImage(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
回复
QQ153984069 2010-09-28
package prj1_1_1;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Midlet1 extends MIDlet implements CommandListener {

private Image[] imageBook;
private DemoCanvas myCanvas;
private Command exitCommand, back;
private Command menuList, select;
private Command backmenu;
private String[] StringArray = { "小猪发脾气", "真假小白兔", "小鼹鼠请客" };
private List listmenu;
private int page = 0;

public Midlet1() throws IOException {
// TODO Auto-generated constructor stub
imageBook = new Image[40];imageBook[0]=Image.createImage("/ima.png");
imageBook[1]=Image.createImage("/f1.png");
imageBook[2]=Image.createImage("/f2.png");
imageBook[3]=Image.createImage("/f3.png");
imageBook[4]=Image.createImage("/f4.png");
imageBook[5]=Image.createImage("/f5.png");
imageBook[6]=Image.createImage("/f6.png");
imageBook[7]=Image.createImage("/f7.png");
imageBook[8]=Image.createImage("/f8.png");
imageBook[9]=Image.createImage("/f9.png");
imageBook[10]=Image.createImage("/f10.png");
imageBook[11]=Image.createImage("/f11.png");
imageBook[12]=Image.createImage("/f12.png");
imageBook[13]=Image.createImage("/f13.png");

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
myCanvas = new DemoCanvas();
Command exitCommand = new Command("退出", Command.EXIT, 2);
Command menuList = new Command("目录列表", Command.SCREEN, 1);
Command back = new Command("Back", Command.EXIT, 1);
Command select = new Command("选择", Command.OK, 1);
Command backmenu = new Command("返回目录列表", Command.BACK, 1);
myCanvas.addCommand(exitCommand);// 载入退出命令
myCanvas.addCommand(menuList);// 载入进入目录列表的命令
myCanvas.addCommand(back);
myCanvas.setCommandListener(this);
listmenu = new List("目录菜单", List.EXCLUSIVE, StringArray, null);
listmenu.addCommand(exitCommand);
listmenu.addCommand(select);
listmenu.addCommand(back);
listmenu.setSelectCommand(select);
listmenu.setCommandListener(this);
listmenu.setFont(0, Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_ITALIC, Font.SIZE_LARGE));
listmenu.setFont(1, Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_ITALIC, Font.SIZE_LARGE));
listmenu.setFont(2, Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_ITALIC, Font.SIZE_LARGE));
Display.getDisplay(this).setCurrent(myCanvas);

}

public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if (c.getCommandType() == Command.EXIT) {
try {
destroyApp(false);

} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
} else if (Command.SCREEN == c.getCommandType()) {
Display.getDisplay(this).setCurrent(listmenu);
} else if (Command.OK == c.getCommandType()) {
System.out.println("选择故事");
int i = listmenu.getSelectedIndex();
page = i + 1;
myCanvas=new DemoCanvas(page);
Display.getDisplay(this).setCurrent(myCanvas);
myCanvas.repaint();
}
}

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

}

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

}
}
回复
QQ153984069 2010-09-28
else if(Command.OK==c.getCommandType())
{
int i=listmenu.getSelectedIndex();
page=i;
myCanvas.repaint();//myCanvas是DemoCanvas型变量
}调用repaint(),但却不会显示调用,我想知道为什么啊?
改为:

else if(Command.OK==c.getCommandType())
{
int i=listmenu.getSelectedIndex();
page=i;
Display.getDisplay(this).setCurrent(myCanvas);
myCanvas.repaint();//myCanvas是DemoCanvas型变量
}

回复
chenzujie 2010-09-28
用了你说的方法结果手机界面出现没多久就消失了,这是什么情况?[Quote=引用 10 楼 tiewantn 的回复:]
没仔细看LZ代码,不过建议把

Java code
Command exitCommand=new Command("退出",Command.EXIT,2);
Command menuList=new Command("目录列表",Command.SCREEN,1);
Command back=new Command("Back",Command.EXIT,1);
Command se……
[/Quote]
回复
colaice2009 2010-09-28
你在list里重绘Canvas怎么可能看到呢,你得返回Canvas啊。
Display.getDisplay(this).setCurrent(myCanvas);
至少得有这句啊。
回复
麦田捕手 2010-09-28
没仔细看LZ代码,不过建议把
Command exitCommand=new Command("退出",Command.EXIT,2);
Command menuList=new Command("目录列表",Command.SCREEN,1);
Command back=new Command("Back",Command.EXIT,1);
Command select=new Command("选择",Command.OK,1);
Command backmenu=new Command("返回目录列表",Command.BACK,1);
myCanvas.addCommand(exitCommand);//载入退出命令
myCanvas.addCommand(menuList);//载入进入目录列表的命令
myCanvas.addCommand(back);
myCanvas.setCommandListener(this);
等写在canvas类中,当然了要把myCanvas替换成this或直接删了myCanvas.也可以,canvas中是可以直接添加Command的,响应commandAction当然也得写在canvas类中了
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2010-09-27 11:57
社区公告
暂无公告