
JavaME repaint() 方法 重绘问题
想要编写一个游戏的开始菜单包括“开始”和“退出”两个菜单。通过按“下方向键”进行选择,选中的菜单会显示为橘黄色。通过绘制图形,进行填充。在模拟器进行操作时,屏幕没有重绘,...
想要编写一个 游戏的 开始菜单 包括 “开始”和“退出” 两个菜单。通过按“下方向键”进行选择,选中的 菜单 会显示 为 橘黄色。通过绘制图形,进行填充。在模拟器进行操作时,屏幕没有重绘,以前填充的窗口还存在。问下怎样才可以使屏幕重绘?原始代码如下。
菜单类:
public class MenuCanvas extends Canvas{
private int canvasWidth;
private int canvasHeight;
private int menuWidth=80;
private int menuHeight=50;
private int fillMenuWidth=menuWidth-2;
private int fillMenuHeight=menuHeight/2-2;
private int recX;
private int recY;
private int[][] fillPoint;
private int curMenu;
public MenuCanvas (){
canvasWidth=getWidth();
canvasHeight=getHeight();
recX=(canvasWidth-menuWidth)/2;
recY=(canvasHeight-menuHeight)/2;
fillPoint=new int[][]{
new int[]{recX+2,recY+2},new int[]{recX+2,recY+fillMenuHeight+4}
};
}
private BounceMIDlet bounceMIDlet;
public MenuCanvas(BounceMIDlet bounceMIDlet){
this();
this.bounceMIDlet=bounceMIDlet;
}
protected void paint(Graphics g) {
g.setColor(244,144,96);
g.fillRect(fillPoint[curMenu][0], fillPoint[curMenu][1], fillMenuWidth, fillMenuHeight);
g.setColor(255,255,255);
g.fillRect(0, 0, canvasWidth, menuHeight);
g.setColor(210,105,30);
g.drawRect(recX, recY, menuWidth, menuHeight);
int lineY=recY+menuHeight/2;
g.drawLine(recX, lineY, recX+menuWidth, lineY);
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD|Font.STYLE_ITALIC,Font.SIZE_LARGE));
g.drawString("开始",canvasWidth/2,recY+3,Graphics.TOP|Graphics.HCENTER);
g.drawString("退出",canvasWidth/2,lineY+3,Graphics.TOP|Graphics.HCENTER);
// TODO Auto-generated method stub
}
protected void keyPressed(int keyCode){
switch(getGameAction(keyCode)){
case UP:
case DOWN:
curMenu=++curMenu%2;
break;
repaint();
}
}
public class BounceMIDlet extends MIDlet {
private Display display;
private MenuCanvas menu;
public BounceMIDlet() {
display=Display.getDisplay(this);
menu=new MenuCanvas(this);
// TODO Auto-generated constructor stub
}
public void exit(){
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 {
display.setCurrent(menu);
// TODO Auto-generated method stub
}
} 展开
菜单类:
public class MenuCanvas extends Canvas{
private int canvasWidth;
private int canvasHeight;
private int menuWidth=80;
private int menuHeight=50;
private int fillMenuWidth=menuWidth-2;
private int fillMenuHeight=menuHeight/2-2;
private int recX;
private int recY;
private int[][] fillPoint;
private int curMenu;
public MenuCanvas (){
canvasWidth=getWidth();
canvasHeight=getHeight();
recX=(canvasWidth-menuWidth)/2;
recY=(canvasHeight-menuHeight)/2;
fillPoint=new int[][]{
new int[]{recX+2,recY+2},new int[]{recX+2,recY+fillMenuHeight+4}
};
}
private BounceMIDlet bounceMIDlet;
public MenuCanvas(BounceMIDlet bounceMIDlet){
this();
this.bounceMIDlet=bounceMIDlet;
}
protected void paint(Graphics g) {
g.setColor(244,144,96);
g.fillRect(fillPoint[curMenu][0], fillPoint[curMenu][1], fillMenuWidth, fillMenuHeight);
g.setColor(255,255,255);
g.fillRect(0, 0, canvasWidth, menuHeight);
g.setColor(210,105,30);
g.drawRect(recX, recY, menuWidth, menuHeight);
int lineY=recY+menuHeight/2;
g.drawLine(recX, lineY, recX+menuWidth, lineY);
g.setColor(0,0,0);
g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD|Font.STYLE_ITALIC,Font.SIZE_LARGE));
g.drawString("开始",canvasWidth/2,recY+3,Graphics.TOP|Graphics.HCENTER);
g.drawString("退出",canvasWidth/2,lineY+3,Graphics.TOP|Graphics.HCENTER);
// TODO Auto-generated method stub
}
protected void keyPressed(int keyCode){
switch(getGameAction(keyCode)){
case UP:
case DOWN:
curMenu=++curMenu%2;
break;
repaint();
}
}
public class BounceMIDlet extends MIDlet {
private Display display;
private MenuCanvas menu;
public BounceMIDlet() {
display=Display.getDisplay(this);
menu=new MenuCanvas(this);
// TODO Auto-generated constructor stub
}
public void exit(){
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 {
display.setCurrent(menu);
// TODO Auto-generated method stub
}
} 展开
展开全部
把下面这段代码
g.setColor(244,144,96);
g.fillRect(fillPoint[curMenu][0], fillPoint[curMenu][1], fillMenuWidth, fillMenuHeight);
放到
int lineY=recY+menuHeight/2;
g.drawLine(recX, lineY, recX+menuWidth, lineY);
这段代码的后面,就可以了.
g.setColor(244,144,96);
g.fillRect(fillPoint[curMenu][0], fillPoint[curMenu][1], fillMenuWidth, fillMenuHeight);
放到
int lineY=recY+menuHeight/2;
g.drawLine(recX, lineY, recX+menuWidth, lineY);
这段代码的后面,就可以了.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询