目前,我正在製作Java程序的圖形可視化Prim的算法尋找最小生成樹。使用定時器在固定的時間重繪,然後繼續計算
Here is the image of my program's output
while(condition){
//Find the min vertex and min edges
Vertex vertex = findMinVertex();
Edge[] edges = findMinEdges();
//Then, for each vertex and edges I found, I will change the color of
//them and pause the program for 3 seconds, so user can see how
//algorithm works.
repaintAndPause(3000);
}
.
.
private void repaintAndPause(int time){
long start = System.currentTimeMillis();
long end = start + speed;
//Here is the timer for repainting.
Timer timer = new Timer(speed, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
GraphPanel.this.repaint();
}
});
timer.setRepeats(false);
timer.setDelay(0);
timer.start();
//And here is for pausing the program, a while loop without any commands.
while(System.currentTimeMillis() < end){}
}
不過,我不知道爲什麼,但該程序無法正常工作。是的,程序有暫停,但所有的邊和頂點只是在程序結束時改變顏色。他們不會每3秒更換一次。
有人能告訴我我做錯了嗎?
謝謝你,祝你有美好的一天!
謝謝AJNeufeld, 你救了我的一天!它像一個魅力! – Lup