My program is to create a bouncing ball that goes up and down and up minus 30% each time and down... in tell the ball has stopped in a resting position.
also I would like to make the ball progressively slow down as it reaches the top of the bonce, and progressively speed up as it descends back to its original position.
So I got the first part set up, I'm just having trouble with not making an infinity loop, and decreasing the up wards y stopping position to decrease by 30% after each bounce.
As I was writing this question I realized, I need to make the y value in the first while loop increase by 30% lintel it reaches 400 correct?
How do I make a loop around the two while loops to repeat over and over, without an infinity loop?
I appreciate any input or comments or ideas!
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;
}
}
}