나는 로봇이 움직이고 탐색 할 수있는 미로가 있습니다. 나는 로봇을 움직일 때 타이머를 사용하려고하지만 타이머는 어떤 이유로 든 걷어차 지 않습니다. 프로그램을 지연시키지 않아서 다시 칠하는 과정을 볼 수 없습니다. 내 코드는 다음과 같습니다.타이머를 사용하여 다시 칠하기
public void updateDrawing(Maze maze) {
// 1000 millisecond delay
Timer t = new Timer(1000, new TimerListener(maze));
t.start();
}
private class TimerListener implements ActionListener {
private Maze maze;
public TimerListener(Maze maze) {
super();
this.maze = maze;
}
public void actionPerformed(ActionEvent e) {
maze.repaint();
}
}
public void explore (int id, Maze maze) {
visited.add(maze.getCell(row, col));
//Loop until we find the cavern
outerloop: //Label the outerloop for breaking purposes
while(!foundCavern){
//Move to a Cell
Cell validCell = chooseValidCell(maze);
//If validCell is null then we have to backtrack till it's not
if(validCell == null){
while(chooseValidCell(maze) == null){
//Go back in route till we find a valid cell
Cell lastCell = route.pollLast();
if(lastCell == null){ //Implies we didn't find cavern, leave route empty
break outerloop;
}
this.row = lastCell.getRow();
this.col = lastCell.getCol();
updateDrawing(maze); // <- this calls repaint using timer
}
//Add back the current location to the route
route.add(maze.getCell(row, col));
validCell = chooseValidCell(maze);
}
this.row = validCell.getRow();
this.col = validCell.getCol();
updateDrawing(maze); // <- this calls repaint using timer
//Add to the route
route.add(validCell);
//Add to visited
visited.add(validCell);
//Check if we're at the cavern
if(row == targetCavern.getRow() && col == targetCavern.getCol()){
foundCavern = true;
}
}
}
누구에게 말해 줄 수 있습니까? 고맙습니다!
당신이 스윙 타이머를 사용하는 방법을 알고 계십니까 ? 나는 프로그램을 바꾸고 싶지 않다. 이 방법은 다른 프로그램에서 작동했습니다. 그것은 간단한 수정이어야합니다. 나는 그것을 고치는 법을 모른다. ... – JOH