2015-11-20 23 views
0

我想製作一個Java彈跳球程序,通過點擊「開始按鈕」可以添加多個球。每個球都在自己的線程中,這意味着10個球實例= 10個線程! 但是,當我嘗試這樣做時,每次創建新線程時,球的速度都會加倍。我的問題: 1.速度翻倍的速度如何? 2.而如何讓每個球在開的螺紋進行操作(10球,10個線程)爪哇彈跳球。爲什麼球的速度每增加一個新的線程就會翻倍?

package test3; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class Test2 extends JFrame{ 
    int width = 1000; 
    int height = 500; 
    BallPanel ballPanel = new BallPanel(); 

    public Test2() { 
     setLayout(new FlowLayout(FlowLayout.CENTER, 0, 10)); 
     JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 

     JButton start = new JButton("Start"); 
     start.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       ballPanel.start(); 
      } 
     }); 
     buttonPanel.add(start); 

     add(buttonPanel); 
     add(ballPanel); 
     ini(); 
    } 

    private void ini(){ 
     setTitle("Test"); 
     setSize(width, height); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    class BallPanel extends JPanel implements Runnable{ 
     int panelWidth = 900; 
     int panelHeight = 370; 
     ArrayList<Ball> ball = new ArrayList<Ball>(); 

     public BallPanel() { 
      setBackground(Color.WHITE); 
      setPreferredSize(new Dimension(panelWidth, panelHeight)); 
     } 

     public void start(){ 
      if (ball.size() < 10) { 
       ball.add(new Ball()); 
      } 

      new Thread(this).start(); 
     } 

     public synchronized ArrayList<Ball> movement(ArrayList<Ball> ball){ 
      for (int i = 0; i < ball.size(); i++) { 
       if (ball.get(i).getCurrentX() < 0 || ball.get(i).getCurrentX() > panelWidth - 10) { 
        ball.get(i).setIncrementX(ball.get(i).getIncrementX() * -1); 
       } else if (ball.get(i).getCurrentY() < 0 || ball.get(i).getCurrentY() > panelHeight - 10) { 
        ball.get(i).setIncrementY(ball.get(i).getIncrementY() * -1); 
       } 

       ball.get(i).setCurrentX(ball.get(i).getCurrentX() + ball.get(i).getIncrementX()); 
       ball.get(i).setCurrentY(ball.get(i).getCurrentY() + ball.get(i).getIncrementY()); 
      } 
      return ball; 
     } 


     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (ball != null) { 
       for (int i = 0; i < ball.size(); i++) { 
        g.setColor(ball.get(i).color); 
        g.fillOval(ball.get(i).getCurrentX(), ball.get(i).getCurrentY(), ball.get(i).getRadius(), ball.get(i).getRadius()); 
       } 
      } 
     } 

     @Override 
     public void run() { 
      while (true) { 
       movement(ball); 
       try { 
        Thread.sleep(5); 
       } catch (InterruptedException e) {} 

       repaint(); 

      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Test2(); 
      } 
     }); 
    } 

} 

球類

package test3; 

import java.awt.Color; 

public class Ball { 
    int currentX; 
    int currentY; 
    int incrementX; 
    int incrementY; 
    int radius; 
    Color color; 



    public Ball() { 
     this.currentX = 7; 
     this.currentY = 0; 
     this.incrementX = 1; 
     this.incrementY = 1; 
     this.radius = 10; 
     this.color = Color.red; 
    } 

    public int getRadius() { 
     return radius; 
    } 

    public int getIncrementX() { 
     return incrementX; 
    } 

    public void setIncrementX(int incrementX) { 
     this.incrementX = incrementX; 
    } 

    public int getIncrementY() { 
     return incrementY; 
    } 

    public void setIncrementY(int incrementY) { 
     this.incrementY = incrementY; 
    } 

    public void setRadius(int radius) { 
     this.radius = radius; 
    } 

    public int getCurrentX() { 
     return currentX; 
    } 

    public void setCurrentX(int currentX) { 
     this.currentX = currentX; 
    } 

    public int getCurrentY() { 
     return currentY; 
    } 

    public void setCurrentY(int currentY) { 
     this.currentY = currentY; 
    } 

    public Color getColor() { 
     return color; 
    } 

    public void setColor(Color color){ 
     this.color = color; 
    } 
} 
+0

您的代碼有效,但我只看到一個線程,當您每個球有一個線程時,代碼如何? –

+2

您的標題應該是一個問題,例如「爲什麼球的速度加倍,我添加的每個線程?」你應該堅持爲這個問題解決單個問題。通過重申您的問題來關閉您的問題主體也是一種很好的形式。這避免了回答者不得不滾動很多東西。良好的大綱是:標題爲問題,你試圖做什麼,你嘗試過什麼,你期望什麼,發生了什麼,問題重述。 –

+0

@Titus,對於這個問題,程序可能應該有一個線程來動畫所有的球。對每個動畫對象使用線程幾乎從不是一個好主意。 –

回答

1

停止()被設置的應用程序= null,並且中斷所述螺紋,但是在run()方法中正在吞噬InterruptedException,所以線程繼續運行。然後,當再次運行start()時,將創建另一個線程,這將導致兩個線程都調用move()方法,使移動速度加倍。

+0

你的回答與我的問題無關。停止方法在這篇文章中並不重要。 –

+0

我也沒有看到與問題中的「停止」方法有關的任何內容。但答案的「核心」是正確的:對於每個添加的球,創建一個新的線程。每個線程將移動*所有*球。所以一般的設計是有點瑕疵... – Marco13

+0

@ Marco13你是一半的權利。我需要的是每增加一個球,就會創建一個新球。每個線程將移動一個「單一」球。 –