0

we're tasked to simulate Prim's algorithm in JavaFX. The idea is, to highlight each step of the algorithm and have a delay of at least 300 milliseconds. So I created these functions.

The first function repaints the AnchorPane such that it removes all components then adds everything relevant to it.

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);  
    }
}

The second function modifies the ArrayList I created that would store temporary vertices and edges that I would use in repainting the anchorpane. Since the code for the prims simulation is long. I'll just show an example.

//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.

After using such, the delay works fine. But the scene doesn't update until after the whole simulatePrims function has been finished. What am I doing wrong? Please help and advice thank you.

Bengemin Uy
  • 53
  • 2
  • 11

0 Answers0