2014-04-25 30 views
1

這是我的第一個圖形化Java程序,我想要做的是重新創建一個簡單的經典程序,其中我彈出一個JFrame窗口中彈出多個球。Java圖形「彈跳球」程序

到目前爲止,我已經成功地使用run()方法中的代碼來反彈。這對我創建的一個球對象起作用,但現在我想要有很多球,所以我正在嘗試在我的Ball類中創建一個方法,這將使我創建的每個球對象獨立地在我的「球世界」中反彈。

現在我所關心的是他們從牆上反彈,而不是彼此(我將在後面解釋)。

問題:在我ballMove(int, int, int, int)方法我有四個int參數,其中,前兩個參數是windowwidthheight,最後兩個參數是XspeedYspeed。當我通過我的if statements時,它會溫和地將xy speed參數設置爲負數,當球擊中右側或底部牆時,但是當run()方法再次執行ballMove(int, int, int, int)方法時,它們會回到正值,並且球從窗口。我曾嘗試在我的球類中使用一堆gettersetter方法。我在我的ballMove(int, int, int, int)方法中嘗試了臨時變量。我沒有嘗試過的作品。

問題:通過使用我的Ball類的方法,如何防止我的參數XspeedYspeed從重新初始化我的實例變量的速度爲正,當球(S)與牆壁碰撞?

因爲我是圖形編程新手,任何有用的建議將不勝感激。

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 

import javax.swing.JFrame; 

public class Main extends JFrame implements Runnable { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int width = 800; 
    private int height= 600; 
    private int ballRadius = 50; 

    private Random rand = new Random(); 

    //Create and initialize a ball object 
    public Ball ball = new Ball(Color.BLUE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500)); 
    //public Ball ball2 = new Ball(Color.RED, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500)); 
    //public Ball ball3 = new Ball(Color.GREEN, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500)); 
    //public Ball ball4 = new Ball(Color.ORANGE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500)); 
    //public Ball ball5 = new Ball(Color.YELLOW, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500)); 
    //constructor 
    public Main(){ 

     setSize(width, height); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

    } 
    //Paint the ball(s) 
    public void paint(Graphics g){ 
     super.paint(g); 
     g.setColor(ball.getColor()); 
     g.fillOval(ball.getBallX(), ball.getBallY(), ball.getWidth(), ball.getHeight()); 
     //g.setColor(ball2.getColor()); 
     //g.fillOval(ball2.getBallX(), ball2.getBallY(), ball2.getWidth(), ball2.getHeight()); 
     //g.setColor(ball3.getColor()); 
     //g.fillOval(ball3.getBallX(), ball3.getBallY(), ball3.getWidth(), ball3.getHeight()); 
     //g.setColor(ball4.getColor()); 
     //g.fillOval(ball4.getBallX(), ball4.getBallY(), ball4.getWidth(), ball4.getHeight()); 
     //g.setColor(ball5.getColor()); 
     //g.fillOval(ball5.getBallX(), ball5.getBallY(), ball5.getWidth(), ball5.getHeight()); 
    } 
    //Run the program 
    public static void main(String [] args){ 
     Main main = new Main(); 
     main.setVisible(true); 
     main.run(); 

    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 


     while(true){ 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      ball.ballMove(width, height, 20, 5); 
      repaint(); 
      //ball2.ballMove(width, height, 15, 3); 
      //repaint(); 
      //ball3.ballMove(width, height, 3, 20); 
      //repaint(); 
      //ball4.ballMove(width, height, 10, 10); 
      //repaint(); 
      //ball5.ballMove(width, height, 10, 20); 
      //repaint(); 


     } 
    } 


} 

這裏是我Ball

import java.awt.Color; 

import javax.swing.JFrame; 


public class Ball extends JFrame { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private int width, height, ball_X, ball_Y; 
    private int Xspeed; 
    private int Yspeed; 
    private Color color; 
    public Ball(Color color, int width, int height, int ball_X, int ball_Y){ 
     this.width = width; 
     this.height = height; 
     this.color = color; 
     this.ball_X = ball_X; 
     this.ball_Y = ball_Y; 
    } 
    public Color getColor(){ 
     return this.color; 
    } 
    public int getWidth(){ 
     return this.width; 
    } 
    public int getHeight(){ 
     return this.height; 
    } 
    public int getBallX(){ 
     return this.ball_X; 
    } 
    public int getBallY(){ 
     return this.ball_Y; 
    } 
    public void setSpeedX(int x){ 
     this.Xspeed = x; 
    } 
    public void setSpeedY(int x){ 
     this.Yspeed = x; 
    } 
    public int getSpeedX(){ 
     return this.Xspeed; 
    } 
    public int getSpeedY(){ 
     return this.Yspeed; 
    } 
    public void setBallX(int x){ 
     this.ball_X = x; 
    } 
    public void setBallY(int y){ 
     this.ball_Y = y; 
    } 
    public void ballMove(int X, int Y, int xSpeed, int ySpeed){ 

     //initialize Xspeed and Yspeed with the parameters of the function 
     this.setSpeedX(xSpeed); 
     this.setSpeedY(ySpeed); 
     //Moves the balls by adding the set speed to the position of the balls each time thread is executed 
     this.setBallX(this.getBallX() + this.getSpeedX()); 
     this.setBallY(this.getBallY() + this.getSpeedY()); 
     //When the balls hit the walls they are suppose to bounce back until they hit another wall. 
     if(this.getBallX() + 50 >= X){ 
      this.setSpeedX(-xSpeed); 
     } 
     if(this.getBallY() + 50 >= Y){ 
      this.setSpeedY(-ySpeed); 
     } 
     if(this.getBallX() + 25 <= 0){ 
      this.setBallX(xSpeed); 
     } 
     if(this.getBallY() + 25 <= 0){ 
      this.setSpeedY(ySpeed); 
     } 
    } 
} 
+1

你爲什麼要在'move()'方法中使用參數。它不應該採取任何參數,只是使用Ball狀態 –

+0

1)使用Swing'Timer'而不是'Thread.sleep()'。 2)你在'this.setSpeedX(xSpeed); this.setSpeedY(ySpeed);'行中的'ballMove()'方法中的問題。 3)在'JFrame'上使用'JPanel'和方法'paintComponent()' – alex2410

+0

@ alex2410'JFrame'和'JPanel'之間的區別是什麼,爲什麼'JPanel'是一個更好的選擇這個?怎麼樣使用Applet類?我可以用它來代替嗎? – zoltack429

回答

0

你的問題是正確的有:

ball.ballMove(width, height, 20, 5);

由於這是一個循環,每次它被稱爲時候你讓它在移動同一方向。如果碰到牆壁,你會在倒數結束時反轉速度,但這並不重要,因爲下一次你稱它時,球仍然向+20,+5移動。

我的建議是在創建球的實例時添加速度參數並讓球更新自己的速度。

ball.ballMove(width, height);

其他建議:其實移動球之前把你的碰撞檢查。這樣,在實際移動它之前,你可以確保你不會走錯方向。

現在,另一個問題是,在你的主體中,你正在調用可運行的run()方法。運行在當前線程上執行。你需要做這樣的事情:

Thread t = new Thread(main); t.start();

獨立於JFrame的做圖紙和計算。

+1

謝謝!我嘗試了你的第一個建議,它完美的工作! – zoltack429