【发布时间】:2017-11-06 10:38:13
【问题描述】:
目前,我正在制作一个用于图形可视化的 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 秒更改一次。
谁能告诉我哪里做错了?
谢谢你,希望你有一个愉快的一天!
【问题讨论】:
标签: java multithreading swing timer visualization