我在做一个汉诺塔的程序,每个盘子都有两个参数,柱号,和层号。而plate在执行paint的时候是根据每个plate的柱号和层号来drawRect盘子的。我在初始化的时候把盘子都画在了第一根柱子上。等我移动了plate1对象以后,plate1对象的柱号和层号都是改变了的,也在新位置画出了plate1对象。但是在原来老位置上的plate1却仍然出现在board上。
我该怎么办才能把原来位置上的plate1对象给搽除掉了?! 代码如下:
主程序:
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.game.*;public class Board extends GameCanvas 
implements CommandListener, Runnable {int pillar1x = 50;
int pillar1y = 175;
int pillar2x = 100;
int pillar2y = 175;
int pillar3x = 150;
int pillar3y = 175;Vector plates1 = new Vector();Vector cursors = new Vector();Font font = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
Random rand = new Random();Thread thread;
Command exitCmd, resetCmd;
MIDlet midlet;class Plate {
int caption;
boolean token;
int attribute;
int index;
int width;
int height = 10;public Plate(int caption) {
this.caption = caption;
}boolean getToken(){
return this.token;
}void setToken(boolean token){
this.token = token;
}void setAttribute(int attribute){
this.attribute = attribute;
} int getAttribute(){
return this.attribute;
}void setIndex(int index){
this.index = index;
} int getIndex(){
return this.index;
}
int getWidth(){
return caption * 10;
} void paint(Graphics g) {
int px;
int py;width = caption * 10;if(attribute == 1){
px = pillar1x - (caption * 5);
py = pillar1y - ((index) * 10) - height;
}
else if(attribute == 2){
px = pillar2x - (caption * 5);
py = pillar2y - ((index) * 10) - height;
}
else {
px = pillar3x - (caption * 5);
py = pillar3y - ((index) * 10) - height; 
}g.setFont(font);
g.drawRect(px, py, width, height);
}
}
public Board(MIDlet midlet_) {
super(true);
midlet = midlet_;
for(int i = 3;i > 0;i--){
Plate p = new Plate(i);
p.setAttribute(1);
if(i == 3){
p.setIndex(0);
}
else if(i == 2){
p.setIndex(1);
}
else{
p.setIndex(2);
}
plates1.addElement(p);
}cursors.addElement(new Cursor());
Cursor cursor1 = (Cursor)cursors.elementAt(0);
cursor1.setPillarNum(1);
cursor1.setIndex(2);
cursor1.setPlateCaption(1);
cursor1.errcount();
exitCmd = new Command("Í˳ö", Command.EXIT, 0);
resetCmd = new Command( "¿ªÊ¼", Command.SCREEN, 1);
addCommand( exitCmd );
addCommand( resetCmd );
setCommandListener(this);
}public void commandAction(Command c, Displayable d) {
if( c==exitCmd ) {
midlet.notifyDestroyed();
}else if( c==resetCmd ) {
resetGrid();
}
}public void paint( Graphics g )
{
flushGraphics();
}
public void paintAll(Graphics g) {
g.drawLine(pillar1x,pillar1y,pillar1x,pillar1y-75);
g.drawLine(pillar2x,pillar2y,pillar2x,pillar2y-75);
g.drawLine(pillar3x,pillar3y,pillar3x,pillar2y-75); for(int i=0;i<plates1.size();i++){
Plate p = (Plate)plates1.elementAt(i);
p.paint(g);
}
errout("plates1.size="+plates1.size());
Cursor cursor1 = (Cursor)cursors.elementAt(0);
cursor1.paint(g);
}
public void showNotify() {
thread = new Thread(this);
thread.start();
}
public void hideNotify() {
thread = null;
}
public void run() {
Graphics g = getGraphics();
Thread mythread = Thread.currentThread();
while (mythread == thread ) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
moveLeft();
System.out.println( "L" );
}
else if ((keyState & RIGHT_PRESSED) != 0) {
moveRight();
System.out.println( "R" );
}
else if ((keyState & FIRE_PRESSED) != 0) {
cursorChange();
System.out.println( "F" );
}
paintAll(g);
flushGraphics();int cnt = 1;
while( getKeyStates() == keyState )
{
try{ Thread.sleep(10);}
catch(Exception e){}
cnt ++;
if( cnt > 100 ) break; 
}
}
}void moveLeft(){
int pIndex;
int plateNum;
int temPIndex;
int temp;
int vecIndex;//
boolean moveAvailable;//
moveAvailable = false;
int minPlate;
minPlate = 100;
Cursor cursor1 = (Cursor)cursors.elementAt(0);
pIndex = cursor1.getPillarNum();
plateNum = cursor1.getPlateCaption();
if(cursor1.tagFlag == true){
temPIndex = pIndex - 1;//
if(temPIndex == 0){
temPIndex = temPIndex + 3;
}
for(int i = 0;i<plates1.size();i++){
Plate p1 = (Plate)plates1.elementAt(i);
if(p1.attribute == temPIndex){ //
if(p1.caption<minPlate){
minPlate = p1.caption;//
}
}
}
if(minPlate != 100){//
for(int i = 0;i<plates1.size();i++){
Plate p2 = (Plate)plates1.elementAt(i);
if(p2.caption == minPlate){//
if(p2.caption>plateNum){//
moveAvailable = false;
}
else{
moveAvailable = true;
}
}
if(moveAvailable == true){//
for(int j = 0;j<plates1.size();j++){
Plate p1 = (Plate)plates1.elementAt(j);
if(p1.attribute == plateNum){
p1.setAttribute(temPIndex);
p1.setIndex(p2.getIndex() + 1);
plates1.removeElementAt(i);
plates1.insertElementAt(p1,j);
cursor1.setPillarNum(temPIndex);
cursor1.setIndex(p2.getIndex() + 1);
}
}
}

}
else{//
for(int j = 0;j<plates1.size();j++){
Plate p1 = (Plate)plates1.elementAt(j);
if(p1.caption == plateNum){
p1.setAttribute(temPIndex);
p1.setIndex(0);
plates1.removeElementAt(j);
plates1.insertElementAt(p1,j);
cursor1.setPillarNum(temPIndex);
cursor1.setIndex(p1.getIndex());
}
}

}
else{
temPIndex = pIndex - 1;
if(temPIndex == 0){
temPIndex = temPIndex + 3;
}
for(int i=0;i<plates1.size();i++ ){
Plate p1 = (Plate)plates1.elementAt(i);
if(p1.attribute == temPIndex){
if(p1.caption<minPlate){
minPlate = p1.caption;
}
}
}
if(minPlate != 100){
for(int j = 0;j<plates1.size();j++){
Plate p1 = (Plate)plates1.elementAt(j);
if(p1.attribute == minPlate){
cursor1.setPillarNum(temPIndex);
cursor1.setIndex(p1.getIndex());
}

}
else{
temPIndex = (pIndex+3)%3;
for(int i=0;i<plates1.size();i++ ){
Plate p1 = (Plate)plates1.elementAt(i);
if(p1.attribute == temPIndex){
if(p1.caption<minPlate){
minPlate = p1.caption;
}
}
}
if(minPlate != 100){
for(int j = 0;j<plates1.size();j++){
Plate p1 = (Plate)plates1.elementAt(j);
if(p1.attribute == minPlate){
cursor1.setPillarNum(temPIndex);
cursor1.setIndex(p1.getIndex());
}

}
}

}//moveLeft finished!void moveRight(){}
void cursorChange(){
Cursor cursor1 = (Cursor)cursors.elementAt(0); 
if(cursor1.tagFlag == true){
boolean f;
f = false; 
cursor1.setTagFlag(f);
}
else{
boolean f;
f = true; 
cursor1.setTagFlag(f);

}}
游标的代码:
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.game.*;class Cursor{
int pillarNum;
int index;
boolean tagFlag;
int plateCaption;
int width;
int height = 10;int pillar1x = 50;
int pillar1y = 175;
int pillar2x = 100;
int pillar2y = 175;
int pillar3x = 150;
int pillar3y = 175;Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN, Font.SIZE_MEDIUM);void setPillarNum(int p){
pillarNum = p;
}
int getPillarNum(){
return pillarNum;

void setIndex(int i){
index = i;
}
void setTagFlag(boolean f){
tagFlag = f;
}
boolean getTagFlag(){
return tagFlag;
}
void setPlateCaption(int i){
plateCaption = i;

int getPlateCaption(){
return plateCaption;
}
void paint(Graphics g){
int px;
int py;if(pillarNum == 1){
px = pillar1x - (plateCaption * 5);
py = pillar1y - ((index) * 10) - height + 5;
}
else if(pillarNum == 2){
px = pillar2x - (plateCaption * 5);
py = pillar2y - ((index) * 10) - height + 5;
}
else {
px = pillar3x - (plateCaption * 5);
py = pillar3y - ((index) * 10) - height + 5; 
}
g.setFont(font);
g.drawLine(px,py,(px+(plateCaption * 10)),py);
}}

解决方案 »

  1.   

    paint方法里面先执行super.paint(g)试试
      

  2.   

    我的类Board继承自GameCanvas,Cursor类继承自Oject,执行super.paint(g)的话。似乎GameCanvas和Oject没有paint这个函数啊!
    我按照2楼说的做了,报错:
    Board.java:103:cannot resolve symbol symbol  : method paint (javax.microedition.lcdui.Graphics) location: class java.lang.Object super.paint(g);和
    Cursor.java:51: cannot resolve symbol symbol  : method paint javax.microedition.lcdui.Graphics) location: class java.lang.Object super.paint(g);
      

  3.   

    你这个程序应该放到j2me区去,还有你的程序从没写注释,读起来费劲。
    给你点思路:(1):不知道j2me里有没有锁的概念你可以给每个盘子加把锁;保证盘子对象的唯一性。
    (2):可能你的paintAll()方法执行时没有覆盖整个jpanel
      

  4.   

    呵呵。楼上的兄弟说的对,这就是个j2me的程序,不过那里好像没什么人气。所以我放到这里来问问。注释到是有,但是转贴过来好像变成了乱码,所以我删除了。
    (1):我不知道有没有锁对象,但是我是用vector来维护的,我确信盘子没有多出来。
    (2):我也觉得问题出在paintAll()上,但是就是看不出来问题在那里。我移动了盘子以后,把各个盘子drawRect的px和py给打印出来了,也是对的。我就搞不懂为什么既然移动盘子成功了,但是原来位置仍然有盘子出现。
      

  5.   

    j2me的api里面木有validate这个函数啊!
      

  6.   

    我知道问题出在那里了,在paintAll()缺少了如下代码:
    g.setColor(0xFFFFFF);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0);
    感谢大家的帮助!