2014-09-10 128 views
0

我的程序是創建一個彈跳球,每上升和下降並上升30%向下......告訴球已停在休息位置。使用while循環彈跳球時,我需要減少每次跳躍30%的彈跳直到0,然後退出循環以避免無限循環

另外我想讓球逐漸減速,因爲它達到了最高點,隨着它回落到原來的位置逐漸加速。

所以我得到了第一部分的設置,我只是遇到了沒有製造無限循環的麻煩,並且在每次反彈之後減少上限30%。

當我寫這個問題時,我意識到,我需要在第一個while循環增加30%門楣時使y值達到400。

我該如何在兩個循環周圍重複一遍又一遍,沒有無限循環?

我很欣賞任何意見或建議!

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JApplet; 

public class MY_Proj04 extends JApplet 
{ 
    int x, y; 
    Color Background; 
    public void init() 
    { 
     x = 100; 
     y = 400; 
     Background = getBackground(); 
    } 

    public void paint(Graphics g) 
    { 
     // I tryed putting a while loop around the two following while loops and 
     // doing y = y * 30/100, I did this because the fill oval can't take a double 
     // as one of its parameters. 

     // 1st while loop 
     while(y >= 0) // Ball goes up to (0,100) 
     { 
      g.setColor(Background); 
      // fill the 500 by 500 square with it background color 
      // any old draw will be covered 
      g.fillRect(0, 0, 500, 500); 
      g.setColor(Color.red); 
      g.fillOval(x, y, 50, 50); 
      for(long i = 1; i < 5000000; i++); //speed of ball 
      y -=1; 
     } 

     // 2nd while loop 
     while(y <= 400) // ball goes down to 400,100 
     { 
      g.setColor(Background); 
      // fill the 500 by 500 square with it background color 
      // any old draw will be covered 
      g.fillRect(0, 0, 500, 500); 
      g.setColor(Color.red); 
      g.fillOval(x, y, 50, 50); 
      for(long i = 1; i < 5000000; i++); //speed of ball 
      y += 1; 
     } 
    } 
} 
+0

看看[這個例子](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets -progressively-不太高在-java的/ 19626396#19626396) – MadProgrammer 2014-09-10 23:53:37

回答

1

我以不同的方式嘗試了這個程序。它的工作。對於球的速度,我用Thread.sleep()而不是你使用的循環。 此外,更正是您代碼中的第一個while循環將球移回起始位置。爲了使其反彈小於先前的高度,您需要增加RHS值(在您的代碼中固定,即0)。還有一件事,爲了讓球看起來像彈跳一樣,我做了一些改變。

下面的代碼:

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

/**<applet code="ball" height = "768" width = "1366" ></applet>*/ 

public class ball extends Applet implements Runnable 
{ 
    int x,y,height,width,a; 
    float z; 
    public void start() 
    { 
     x=100; y=400; h=50; w=50; a=0; 
     z = y; 
     Thread th=new Thread(this); 
     th.start(); 
    } 

    public void run() 
    { 
     while(Math.round(z)!=0) 
     { 
      while(y>=a) //Ball bounces till it reaches point a. Which in turn is increasing with each bounce till it is equal to 400. 
      { 
       y--; 
       repaint(); 
       try{Thread.sleep(3);} // Speed of the ball. 
       catch(Exception e){} 
      } 
      while(y<=400) // This loop gives ball a bouncing look each time it hits the floor (i.e. 400). 
      { 
       y++; 
       repaint(); 
       if(y==400) 
       { 
        while(h!=40) 
        { 
         y++; 
         height--; 
         x--; 
         width+=2; 
         repaint(); 
         try{Thread.sleep(3);} 
         catch(Exception e){} 
        } 
        while(h!=50) 
        { 
         y--; 
         height++; 
         x++; 
         widt--=2; 
         repaint(); 
         try{Thread.sleep(3);} 
         catch(Exception e){} 
        } 
       } 
       try{Thread.sleep(3);} 
       catch(Exception e){} 
      } 
      z=z*(0.7f); //This line and the next line calculates the value of a for the next bounce. 
      a=Math.round(y-z); //round() function rounds the   floating value to nearest whole number and converts it into an integer. 
     } 
    } 

    public void paint(Graphics g) 
    { 
     g.fillOval(x,y,width,height); 
    } 
}