I'm trying to write a program that bounces a ball continuously inside a window for homework assignment. Here is a short youtube video of what I'm trying to do. As the program is written right now the ball does not move and I don't understand why.
I have two questions:
- What am I doing incorrectly for why the ball is not moving?
- Should I use the >= and <= operators instead of the > and < operators that I used inside of the IF (conditions)?
import acm.program.*;
import acm.graphics.*;
publc class BallBouncing extends GraphicsProgram{
public void run(){
GOval ball = new GOval(getWidth() / 2, getHeight() / 2, OVAL_SIZE, OVAL_SIZE);
ball.setFilled(true);
add(ball);
double dx = getWidth() / N_STEPS;
double dy = getHeight() / N_STEPS;
while(true){
ball.move(dx, dy);
pause(PAUSE_TIME);
if (ball.getY() > getHeight() - OVAL_SIZE) { dy = dy * -1;}
if (ball.getX() > getWidth() - OVAL_SIZE) { dx = dx * -1;}
if (ball.getY() < 0 + OVAL_SIZE) { dy = dy * -1;}
if (ball.getX() < 0 + OVAL_SIZE) { dx = dx * -1;}
}
}
private static final int STEPS = 1000;
private static final int PAUSE_TIME = 5;
private static final int OVAL_SIZE = 25;
}