2015-11-19 61 views
1

我正試圖製作一個小應用程序,可以在窗口框架周圍彈跳球。當我給列表添加多於一個的球時,它不像預期的那樣循環它們。爲什麼我的ArrayList沒有遍歷所有值?

這裏是我的代碼:

主類:

public class Main extends JFrame { 

private static final long serialVersionUID = 1L; 
private List<Ball> balls = new ArrayList<Ball>(); 
private List<Ball> tempballs = new ArrayList<Ball>(); 
private static Timer t; 

public void addBall(Ball b) { 
    tempballs.add(b); 
} 

public void initUI() { 
    this.setSize(500, 500); 
    this.setDefaultCloseOperation(3); 
    this.setVisible(true); 
} 

private BufferStrategy bs; 
private Random r = new Random(); 
public void paint() { 
    if (!tempballs.isEmpty()) { 
     balls.addAll(tempballs); 
     tempballs = new ArrayList<Ball>(); 
    } 
    int i = 0; 
    System.out.println(balls.size()); 
    for (Ball b : new ArrayList<Ball>(balls)) { 
     i++; 
     System.out.println(i); 

     if ((bs = this.getBufferStrategy()) == null) { 
      this.createBufferStrategy(2); 
      return; 
     } 

     if (bs.contentsLost() || bs.contentsRestored()) { 
      return; 
     } 
     if (b.y >= this.getHeight() - 100) { 
      b.ydirection = -r.nextDouble() * 5; 
     } 
     if (b.y < 20) { 
      b.ydirection = r.nextDouble() * 5; 
     } 
     if (b.x >= this.getWidth() - 100) { 
      b.xdirection = -r.nextDouble() * 5; 
     } 
     if (b.x < 0) { 
      b.xdirection = r.nextDouble() * 5; 
     } 

     b.x += b.xdirection; 
     b.y += b.ydirection; 

     if (b.xdirection > 0) 
      b.xdirection += 0.1; 
     else 
      b.xdirection += -0.1; 

     if (b.ydirection > 0) 
      b.ydirection += 0.1; 
     else 
      b.ydirection += -0.1; 

     Graphics g = bs.getDrawGraphics(); 

     g.fillOval((int) b.x, (int) b.y, 100, 100); 

     bs.show(); 

     g.dispose(); 

     bs.dispose(); 

    } 
    i = 0; 

} 

public static void main(String[] args) { 
    try { 
     final Main m = new Main(); 
     m.addMouseListener(new Mouse(m)); 
     m.initUI(); 
     t = new Timer(); 
     TimerTask tt = new TimerTask() { 

      @Override 
      public void run() { 
       m.paint(); 
      } 
     }; 
     t.schedule(tt, Calendar.getInstance().getTime(), 20); 
    } catch (ConcurrentModificationException e) { 
     e.printStackTrace(); 
    } 
} 

}

這裏的球類:

public class Ball { 

private Random r = new Random(); 
public double y, x, ydirection, xdirection; 
public Ball(int x, int y) { 
    this.y = y; 
    this.x = x; 
    ydirection = r.nextGaussian() * 5; 
    xdirection = r.nextGaussian() * 5; 
} 

}

和鼠標監聽器:

public class Mouse implements MouseListener { 
Main m; 
public Mouse(Main m) { 
    this.m = m; 
} 

@Override 
public void mouseClicked(MouseEvent e) { 
    m.addBall(new Ball(e.getX(), e.getY())); 
    System.out.println("cl"); 

} 
@Override 
public void mouseEntered(MouseEvent arg0) { 

} 

@Override 
public void mouseExited(MouseEvent arg0) { 

} 

@Override 
public void mousePressed(MouseEvent arg0) { 

} 

@Override 
public void mouseReleased(MouseEvent arg0) { 

} 

}

其他細節:

  • 它只遍歷列表中的第一個項目,但列表規模的增長。
  • 我使用的是Java 6,但如果需要,我會更改版本。
+0

爲什麼要創建一個新的'ArrayList '作爲'balls'的副本?它不應該導致問題,但它無論如何都是奇怪的。 –

+0

你正在重畫嗎?你如何檢查球沒有被纏繞? – matt

+0

這正是調試器的功能......沒人教過如何調試嗎? – redFIVE

回答

1

如果您的循環沒有按預期運行,請嘗試找出它被取消的原因。您的循環中有兩個return語句可能導致此行爲。在這些返回之前進行sysout以找出哪一個是原因。然後找出爲什麼你的回報是真實的。要深入挖掘,您可以使用IDE的調試模式,並將斷點放在有趣的行中,或者使用步進模式一次運行一行代碼。

除此之外,您可以在循環之前放置if,當您處於paint()函數(您正在使用可能會更改它們的UI線程)時,它們檢查的值不應更改。

+0

我已經採納了您的建議,並將所有圖形代碼移出循環。它工作正常!我意識到我現在做錯了什麼。非常感謝! – NameNotFoundException

1

您在循環內有低於return的語句。如果執行到達return語句中的任何一個,則循環結束。

找出來,如果您輸入這些條件並返回列表中的第一個值。

if ((bs = this.getBufferStrategy()) == null) { 
      this.createBufferStrategy(2); 
      return; 
     } 

     if (bs.contentsLost() || bs.contentsRestored()) { 
      return; 
     }