我们的任务是在JavaFX中模拟Prim的算法。 该想法是突出算法的每个步骤并且具有至少300毫秒的延迟。 所以我创建了这些函数。
第一个函数重新绘制AnchorPane,以便删除所有组件,然后添加与之相关的所有组件。
private void repaintGraphViewTab() {
graphPane.getChildren().clear();
for(int counter = 0; counter < temporaryLines.size(); counter++) {
Double sourceXPos = temporaryLines.get(counter).getSource().getXPosition();
Double sourceYPos = temporaryLines.get(counter).getSource().getYPosition();
Double destXPos = temporaryLines.get(counter).getDestination().getXPosition();
Double destYPos = temporaryLines.get(counter).getDestination().getYPosition();
Line currentLine = new Line(sourceXPos, sourceYPos, destXPos, destYPos);
currentLine.setSmooth(true);
currentLine.setDisable(true);
currentLine.setStrokeWidth(1);
currentLine.setStroke(simulatedColor);
graphPane.getChildren().add(currentLine);
}
for(int counter = 0; counter < temporaryPoints.size(); counter ++) {
Circle currentPoint = new Circle(temporaryPoints.get(counter).getXPosition(),temporaryPoints.get(counter).getYPosition(),5);
currentPoint.setFill(simulatedColor);
currentPoint.setSmooth(true);
currentPoint.setDisable(true);
graphPane.getChildren().add(currentPoint);
}
}
第二个函数修改我创建的ArrayList,它将存储我将用于重新绘制anchorpane的临时顶点和边。由于prims模拟的代码很长。我只是举个例子。
//After sorting the current edges traversable by ascending weight.
Edge currentLowestWeightEdge = adjacentEdges.remove(0);
temporaryLines.add(currentLowestWeightEdge);
repaintGraphViewTab();
Thread.sleep(300);
//Where this is called every time there is a need to be changed with the temporary lines or points arraylist.
使用后,延迟正常。但是在整个simulatePrims函数完成之后,场景才会更新。我究竟做错了什么?请帮助和建议,谢谢。