2013-01-10 51 views
0

我在模擬兩個競爭者之間的比賽時遇到了問題。這是您典型的比賽項目,您可以使用隨機數字生成器來確定競爭對手使用的「動作」。正如我在下面的代碼中看到的那樣,賽道由50個矩形組成,填充的矩形顯示每個參賽者在賽道上的位置。例如,某些「移動」會使競爭對手向右跳9格,或向後跳2格。當我運行小程序時,僅顯示初始起始位置;該applet不起作用。我意識到這是很多代碼,但我需要做什麼來解決我的問題?我真的被困在這一點上。任何幫助表示讚賞。我只能使用AWT,而不能揮杆。這是類的分配:/這裏是代碼:Java比賽小程序不能正常工作

import java.awt.*; 
import java.applet.*; 


    public class Example extends Applet 
    { 
     Image tortoise, hare; 
     int tortX = 250, hareX = 250; 
     final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50; 
     int turn; String turnNum; 
     int move; String tMove, hMove; 

     public void init() 
     { 
      tortoise = getImage(getDocumentBase(), "images/tortoise.gif"); 
      hare = getImage(getDocumentBase(), "images/hare.gif"); 
      move = 0; turn = 0; 
     } 


     public void control() 
     { 
      while ((tortX < 985) || (hareX < 985)) 
      { 
       move = (int)(10 * Math.random()); 
       switch (move) 
       { 
        case 1: 
        case 2: 
         tortX += (3 * WIDTH); 
         hareX += (9 * WIDTH); 
         tMove = "Fast Plod"; hMove = "Big Hop"; 
         break; 
        case 3: 
        case 4: 
        case 5: 
         tortX += (3 * WIDTH); 
         hareX += WIDTH; 
         tMove = "Fast Plod"; hMove = "Small Hop"; 
         break; 
        case 6: 
         tortX += WIDTH; 
         if (hareX == 250) {} // DO NOTHING 
         else if (hareX <= (250 + (11 * WIDTH))) 
          hareX = 250; 
         else 
          hareX -= (12 * WIDTH); 
         tMove = "Slow Plod"; hMove = "Big Slip"; 
         break; 
        case 7: 
        case 8: 
         tortX += (1 * WIDTH); 
         if (hareX == 250) {} // DO NOTHING 
         else if (hareX <= (250 + (WIDTH))) 
          hareX = 250; 
         else 
          hareX -= (2 * WIDTH); 
         tMove = "Slow Plod"; hMove = "Small Slip"; 
         break; 
        case 9: 
        case 10: 
         if (tortX == 250) {} // DO NOTHING 
         else if (tortX <= (250 + (5 * WIDTH))) 
          tortX = 250; 
         else 
          tortX -= (6 * WIDTH); 
         tMove = "Slip"; hMove = "Fall Asleep. Zzz..."; 
         break; 
         // Hare falls asleep. No action. 
       } 

       turn++; turnNum = (turn + ""); 
       repaint(); 
       for (int i = 1; i <= 10; i++) 
       { 
        delay(); 
       } 
      } 

      tortX = 985; hareX = 985; 
      repaint(); 
     } 

     public void paint(Graphics screen) 
     { 
      drawRace(screen); 

      if (tortX >= 985) 
      { 
       screen.setFont(new Font("Times New Roman", Font.ITALIC, 48)); 
       screen.drawString("Tortoise Wins", 650, 240); 
       clearCurrent(screen); 
       fillNext(screen); 
      } 
      else if (hareX >= 985) 
      { 
       screen.setFont(new Font("Times New Roman", Font.ITALIC, 48)); 
       screen.drawString("Tortoise Wins", 650, 240); 
       clearCurrent(screen); 
       fillNext(screen); 
      } 
      else 
      {  
       screen.drawString(("Turn " + turnNum), 621, 55); 
       screen.setFont(new Font("Times New Roman", Font.ITALIC, 12)); 
       screen.drawString(tMove, 59, 65); screen.drawString(hMove, 66, 255); 
       clearCurrent(screen); 
       fillNext(screen); 
      } 

      stop(); 
     } 

     public void clearCurrent(Graphics s) 
     { 
      s.clearRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1); 
      s.clearRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1); 

     } 

     public void fillNext(Graphics s) 
     { 
      s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1); 
      s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1); 

     } 

     public void drawRace(Graphics s) 
     { 
      // GENERATES INITIAL GRAPHICS FOR RACE 
      s.drawRect(250, 100, 750, 50); 
      s.drawRect(250, 300, 750, 50); 
      int lineX = 265, lineYi = 100, lineYf = 150; 
      for (int i = 1; i <= 98; i++) 
      { 
       if (lineX == 1000) 
       { 
        lineX = 265; lineYi = 300; lineYf = 350; 
       } 
       s.drawLine(lineX, lineYi, lineX, lineYf); 
       lineX += 15; 
      } 
      s.fillRect(tortX+1, tortY+1, WIDTH-1, HEIGHT-1); 
      s.fillRect(hareX+1, hareY+1, WIDTH-1, HEIGHT-1); 
      s.drawImage(tortoise, 59, 80, this); 
      s.drawImage(hare, 66, 271, this); 
      s.setFont(new Font("Times New Roman", Font.BOLD, 24)); 
      s.drawString("Race", 250, 55); 
     } 

     public void delay() 
     { 
      for (int i = 0; i < 90000000; i++) 
      { 
      } 
     } 

     public void stop() 
     { 
     } 
    } 

回答

3

你的第一個問題是,你卻從未「開始」比賽......當然你init小程序,不過呢,沒什麼......

你的第二個問題是control方法是阻塞事件調度線程,這意味着,他們雖然在這個方法中沒有任何東西會被繪製到屏幕上。這是因爲事件分派線程也負責分派重新繪製請求。

您的第三個問題是您違反了paint聯繫人。你有責任打電話給super.paint(screen) - 油漆是一種複雜的方法,除非你有充分的理由這麼做,否則不應該忽視它。

第四個問題是,您使用的是Applet而不是JApplet。最好不要忽視AWT控件而選擇Swing控件。 Swing更靈活,更容易擴展。

你的第五個問題是你繪製到頂級容器上,這是從來不推薦的。你最好使用類似JPanel的東西,並重寫它的paintComponent方法(不要忘記打電話super.paintComponent)。除了其他所有功能之外,它還具有雙緩衝功能,並可在屏幕更新時減少輕彈。

看一看......

+0

我assignemt說,我們要使用AWT,因爲我們還沒有學習JApplet的Swing:/ – user1947236

+0

那麼,其他所有的東西都是:P – MadProgrammer