是这样的,我现在要做一个程序,每次计算完路径之后都要repaint()一次,以便显示这次走的格子和路径,总共40个回合,就要显示40次,但是现在我用Thread.sleep(),程序锁死,用timer的话,直接repaint最后一步,之前的显示不出来,我设的延时参数都是500ms.下面是我按完start按钮之后计算的执行的部分……
DPanel.update()是repaint()命令……
private class StartAction extends Thread implements ActionListener
{
run r;
ChasingCatStrDog Strd;
public void actionPerformed(ActionEvent event)
{
eat = 0;
count = 0;
ex = 0;
java.util.Timer timer = new java.util.Timer();
timer.schedule(new TimerTask()
{
public void run()
{
label1: while(count < turn && eat == 0 && ex == 0)
{
r = new run(Str, c, d, grid);
Strd = new ChasingCatStrDog(c, d);
System.out.println("turn " + count +" "+ c.catx +" "+ c.caty);
DPanel.update();
if (c.catx == gate && c.caty == 0)
{
JDialog exDialog = new exitDialog(UIFrame.this);
exDialog.setVisible(true);
ex = 1;
break label1;
}
count++;
for(int i = 0; i < dn; i++)
{
if((c.catx == d[i].dogx) && (c.caty == d[i].dogy))
{
JDialog eDialog = new eatDialog(UIFrame.this);
eDialog.setVisible(true);
eat = 1;
break label1;
}
}
}
} },500,500);
}
}
DPanel.update()是repaint()命令……
private class StartAction extends Thread implements ActionListener
{
run r;
ChasingCatStrDog Strd;
public void actionPerformed(ActionEvent event)
{
eat = 0;
count = 0;
ex = 0;
java.util.Timer timer = new java.util.Timer();
timer.schedule(new TimerTask()
{
public void run()
{
label1: while(count < turn && eat == 0 && ex == 0)
{
r = new run(Str, c, d, grid);
Strd = new ChasingCatStrDog(c, d);
System.out.println("turn " + count +" "+ c.catx +" "+ c.caty);
DPanel.update();
if (c.catx == gate && c.caty == 0)
{
JDialog exDialog = new exitDialog(UIFrame.this);
exDialog.setVisible(true);
ex = 1;
break label1;
}
count++;
for(int i = 0; i < dn; i++)
{
if((c.catx == d[i].dogx) && (c.caty == d[i].dogy))
{
JDialog eDialog = new eatDialog(UIFrame.this);
eDialog.setVisible(true);
eat = 1;
break label1;
}
}
}
} },500,500);
}
}
楼主【jeffsyr】截止到2008-07-24 12:07:22的历史汇总数据(不包括此帖):
发帖的总数量:1 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:0
结贴的百分比:0.00 % 结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
private class StartAction extends Thread implements ActionListener
{
run r;
ChasingCatStrDog Strd;
public void actionPerformed(ActionEvent event)
{
eat = 0;
count = 0;
ex = 0;
Thread t = new Thread();
t.start();
label1: while(count < turn && eat == 0 && ex == 0)
{
try
{
r = new run(Str, c, d, grid);
Strd = new ChasingCatStrDog(c, d);
System.out.println("turn " + count +" "+ c.catx +" "+ c.caty);
DPanel.update();
t.sleep(speedvalue);
if (c.catx == gate && c.caty == 0)
{
JDialog exDialog = new exitDialog(UIFrame.this);
exDialog.setVisible(true);
ex = 1;
break label1;
}
count++;
for(int i = 0; i < dn; i++)
{
if((c.catx == d[i].dogx) && (c.caty == d[i].dogy))
{
JDialog eDialog = new eatDialog(UIFrame.this);
eDialog.setVisible(true);
eat = 1;
break label1;
}
}
}
catch(InterruptedException e)
{
}
}
} }
}