2014-12-03 4 views
0

나는 로봇이 움직이고 탐색 할 수있는 미로가 있습니다. 나는 로봇을 움직일 때 타이머를 사용하려고하지만 타이머는 어떤 이유로 든 걷어차 지 않습니다. 프로그램을 지연시키지 않아서 다시 칠하는 과정을 볼 수 없습니다. 내 코드는 다음과 같습니다.타이머를 사용하여 다시 칠하기

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

누구에게 말해 줄 수 있습니까? 고맙습니다!

답변

-1

다음은 기본 타이머를 만드는 방법입니다. 방금에서이 빼기, 당신은 시간을 표시 할 때, 나중에

long startTime = System.currentTimeMillis(); 

:

하여 표시하는 시간을 계산해야 할 것은, 타이머 시작 시간을 기록하는 것입니다 현재 시간

long elapsedTime = System.currentTimeMillis() - startTime; 
long elapsedSeconds = elapsedTime/1000; 
long secondsDisplay = elapsedSeconds % 60; 
long elapsedMinutes = elapsedSeconds/60; 
//put here code to format and display the values 

당신이 원하는 값을 elapsedSeconds == 때까지 기다릴 수 있습니다. Make a simple timer in Java

+0

당신이 스윙 타이머를 사용하는 방법을 알고 계십니까 ? 나는 프로그램을 바꾸고 싶지 않다. 이 방법은 다른 프로그램에서 작동했습니다. 그것은 간단한 수정이어야합니다. 나는 그것을 고치는 법을 모른다. ... – JOH

0

시도를 사용하지 ** updateDrawing (미로) **에서

그러나이 방법

무효 updateMaze는() {

EventQueue.invokeLater(()->updateDrawing(maze)); 
} 
+0

고맙다. 그러나 그것은 작동하지 않았다. ... 타이머는 아직 시작되지 않는다. – JOH

관련 문제