2012-04-05 71 views
1

我是一個完整的java noob。我仍然不知道如何編碼。 我在迷宮中遇到問題。我現在可以從開始(2)結束(3),但問題是(開始)的圖標。我試圖打印出我們的迷宮(mazePlan)的值,並且很好。唯一的問題是圖標。遞歸函數完成後,圖標才改變(我使用setIcons)。我需要讓這些圖標開始移動。謝謝!我真的需要一些幫助。謝謝!Java:迷宮圖標:該怎麼辦?

class solveButton implements ActionListener{ 
    private boolean goal; 
    public void actionPerformed(ActionEvent e1) 
    { 
     int i,j, a = 0,b = 0; 
     goal = false; 

     //Find 2/start 
     for(i = 0; i<10; i++){ 
      for (j = 0; j<10; j++){ 
       if (getMazePlan()[i][j] == 2){ 
        a= i; b = j; 
       }  
      } 
     } 

     function(a,b); 

     //print only 
     for (i=0;i<10;i++){ 
      for(j = 0; j < 10; j++){ 
       System.out.print(getMazePlan()[i][j] + " "); 
      } 
      System.out.println(); 
     } 

    } 

    public void function(int y, int x){ 

     int ctr = 0,b ,a; 

     //CHECKING FOR WALLS 
     //LEFT 
      if(x-1 >= 0 && x-1 <= 9){ 
       if(getMazeBoolean()[y][x-1] == false) 
        ctr++; 
      } 
     //UP 
      if(y-1 >= 0 && y-1 <= 9){ 
       if(getMazeBoolean()[y-1][x] == false) 
        ctr++; 
      } 
     //RIGHT 
      if(x+1 >= 0 && x+1 <= 9){ 
       if(getMazeBoolean()[y][x+1] == false) 
        ctr++; 
      } 
     //DOWN 
      if(y+1 >= 0 && y+1 <= 9){ 
       if(getMazeBoolean()[y+1][x] == false) 
        ctr++; 
      } 

     try{ 
      Thread.sleep(1000); 
     }catch(Exception e){} 

      //print only 
     System.out.println("Move"); 
     for (int i=0;i<10;i++){ 
      for(int j = 0; j < 10; j++){ 
       System.out.print(getMazePlan()[i][j] + " "); 
      } 
      System.out.println(); 
     } 

     stack1.push(x); 
     stack1.push(y); 
     setMazePlan1(0, y, x); 
     maze[y][x].setIcon(end); 
     setMazeBoolean(true, y , x); 


     //LEFT 
     if(ctr > 0 && goal == false){ 
     if(x-1 >= 0 && x-1 <= 9){ 
      if(getMazeBoolean()[y][x-1] == false && getMazePlan()[y][x-1] != 3){ 
       setMazePlan1(2, y, x-1); 
       function(y, x-1); 
       ctr--; 

       if(ctr != 0 && goal == false){ 
        b = stack1.pop(); 
        a = stack1.pop(); 
        setMazePlan1(2, b , a); 
        stack1.push(a); 
        stack1.push(b); 

        try{ 
         Thread.sleep(1000); 
        }catch(Exception e){} 

        setMazePlan1(0, b , a); 
       } 
      } 
      else if(getMazePlan()[y][x-1] == 3){ 
       System.out.println("FINISH"); 
       goal = true; 
      } 
     } 
     } 
     //UP 
     if(ctr > 0 && goal == false){ 
     if(y-1 >= 0 && y-1 <= 9){ 
      if(getMazeBoolean()[y-1][x] == false && getMazePlan()[y-1][x] != 3){ 
       setMazePlan1(2, y-1, x); 
       function(y-1, x); 
       ctr--; 

      if(ctr != 0 && goal == false){ 
       b = stack1.pop(); 
       a = stack1.pop(); 
       setMazePlan1(2, b , a); 
       stack1.push(a); 
       stack1.push(b); 

       try{ 
        Thread.sleep(1000); 
       }catch(Exception e){} 

       setMazePlan1(0, b , a); 
      } 
      } 
      else if(getMazePlan()[y-1][x] == 3){ 
       System.out.println("FINISH"); 
       goal = true; 
      } 
     } 
     } 
     //RIGHT 
     if(ctr > 0 && goal == false){ 
     if(x+1 >= 0 && x+1 <= 9){ 
      if(getMazeBoolean()[y][x+1] == false && getMazePlan()[y][x+1] != 3){ 
       setMazePlan1(2, y, x+1); 
       function(y, x+1); 
       ctr--; 

      if(ctr != 0 && goal == false){ 
       b = stack1.pop(); 
       a = stack1.pop(); 
       setMazePlan1(2, b , a); 
       stack1.push(a); 
       stack1.push(b); 

       try{ 
        Thread.sleep(1000); 
       }catch(Exception e){} 

       setMazePlan1(0, b , a); 
      } 
      } 
      else if(getMazePlan()[y][x+1] == 3){ 
       System.out.println("FINISH"); 
       goal = true; 
      } 
     } 
     } 
     //DOWN 
     if(ctr > 0 && goal == false){ 
     if(y+1 >= 0 && y+1 <= 9){ 
      if(getMazeBoolean()[y+1][x] == false && getMazePlan()[y+1][x] != 3){ 
       setMazePlan1(2, y+1, x); 
       function(y+1, x); 
       ctr--; 

      if(ctr != 0 && goal == false){ 
       b = stack1.pop(); 
       a = stack1.pop(); 
       setMazePlan1(2, b , a); 
       stack1.push(a); 
       stack1.push(b); 

       try{ 
        Thread.sleep(1000); 
       }catch(Exception e){} 

       setMazePlan1(0, b , a); 
      } 
      } 
      else if(getMazePlan()[y+1][x] == 3){ 
       System.out.println("FINISH"); 
       goal = true; 
      } 
     } 
     } 
     //DEADEND 
     if(goal == false && ctr == 0){ 

      b = stack1.pop(); 
      a = stack1.pop(); 
      setMazePlan1(0, b , a); 
      try{ 
      b = stack1.pop(); 
      a = stack1.pop(); 
      stack1.push(a); 
      stack1.push(b); 
      setMazePlan1(2, b , a); 
      } 
      catch(Exception e){ 

      } 

      try{ 
       Thread.sleep(1000); 
      }catch(Exception e){} 

      //print only 
      System.out.println("Pop"); 
      for (int i=0;i<10;i++){ 
       for(int j = 0; j < 10; j++){ 
        System.out.print(getMazePlan()[i][j] + " "); 
       } 
       System.out.println(); 
      } 
     } 

    } 
} 
+1

對於遲到的回覆感到抱歉。 我讀過Swing Timers並嘗試使用它。它的工作原理! :) 好極了 !非常感謝你 – Kopaka 2012-04-08 07:15:01

回答

1

不要阻塞EDT(Event Dispatch Thread) - 當發生這種情況時GUI將「凍結」。而不是致電 Thread.sleep(n)實施重複任務的Swing Timer或長時間運行任務的SwingWorker。 有關更多詳細信息,請參閱Concurrency in Swing

0

沒有試圖破譯你的大塊代碼,聽起來像需要在設置圖標後強制重繪/繪畫。

沒有真正足夠的代碼在這裏知道什麼叫重繪()...

你可能也想看看Java的GUI應用程序/ GUI線程(即你不應該做所有如果可能需要一段時間,則在GUI線程中顯示「迷宮」內容)

+0

抱歉的代碼...試圖重畫圖標,面板和框架,仍然無法正常工作 – Kopaka 2012-04-05 03:29:23