I need the ball to move down until it hits the end of the blue rectangle then move back up until it hits the top and repeat forever. What's wrong with my code?
//This applet draws an animated bouncing ball with uniform speed.
//The ball starts at the center of the graphics page and bounces off
//the top and bottom
import java.awt.*;
import java.applet.Applet;
public class BouncingBall extends Applet {
//Dimensions of Applet:
public final int width = 500;
public final int height = 500;
//Size and speed of ball:
public final int diameter = 20;
public final int speed = 5;
//Milliseconds to pause between frames:
public final int pause = 20;
int y = 250;
public void paint (Graphics page) {
page.setColor(Color.blue);
page.fillRect(0,0,500,500);
page.setColor(Color.red);
page.fillOval(250 - 10, y - 10, 20, 20);
//Loop forever;
}
public void run ()
{
while (true) {
if (y<=450){
y--;
repaint();
}
else if (y>=10){
y++;
repaint();
}
//Pause between frames:
try { Thread.sleep(pause) ; } catch (Exception e) { };
}
}
}
Thanks