Этот код визуализирует алгоритм A *. Когда он достигает конца алгоритма, он выдает предупреждение о том, что путь найден с помощью
JOptionPane.showMessageDialog(null, "Path Found")
. После щелчка мышью windows исчезает, но затем снова отображается в верхнем левом углу без опции закрыть это. Кроме того, путь к цели отображается после щелчка, однако в коде он отображается до появления предупреждения. Я уверен, что проблема в этом методе именно в том, как я рисую или вызываю метод перекраски, а не в самом алгоритме. Может кто-нибудь объяснить, где я не так?
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.renderGrid(g);
this.renderCloseCells(g);
this.renderOpenCells(g);
if (aStar.getOpenSet().size() > 0) {
int lowestCostIndex = 0;
for (int i = 0; i < aStar.getOpenSet().size(); i++){
if (aStar.getOpenSet().get(i).getF() < aStar.getOpenSet().get(lowestCostIndex).getF()){
lowestCostIndex = i;
}
}
Cell current = aStar.getOpenSet().get(lowestCostIndex);
if (aStar.getOpenSet().get(lowestCostIndex) == aStar.getEnd()){
Cell temp = current;
aStar.addItemPath(temp);
while (temp.getParent() != null){
aStar.addItemPath(temp.getParent());
temp = temp.getParent();
}
System.out.println("Done");
this.renderPath(g);
JOptionPane.showMessageDialog(null, "Path Found");
return;
}
aStar.removeCellOpenSet(current);
aStar.addCellCloseSet(current);
for (int i = 0; i < current.getNeighbors().size(); i++){
Cell neighbor = current.getNeighbors().get(i);
if (!aStar.getCloseSet().contains(neighbor)){
int tempG = current.getG() + 1;
if (aStar.getOpenSet().contains(neighbor)) {
if(tempG < neighbor.getG()){
neighbor.setG(tempG);
}
} else {
neighbor.setG(tempG);
aStar.addCellOpenSet(neighbor);
}
neighbor.setH(aStar.heuristic(neighbor, aStar.getEnd()));
neighbor.setF(neighbor.getG() + neighbor.getH());
neighbor.setParent(current);
}
}
} else {
JOptionPane.showMessageDialog(null, "Path Not Found!");
}
this.repaint();
}