import Myro.*; public class Ball { private MyroCanvas myCanvas; private static final double GRAVITY = 0.98; private static final double ENERGYLOSS = 0.8; private static final int RADIUS = 10; private static final double DELAY = 0.05; private MyroCircle myCircle; private double xPos; private double yPos; private int maxX; private int maxY; public Ball(MyroCanvas gamesCanvas, int canvasX, int canvasY, int r, int g, int b, int startXLocation, int startYLocation) { myCanvas = gamesCanvas; double overBounce; boolean notDone = true; xPos = startXLocation; yPos = startYLocation; maxX = canvasX; maxY = canvasY; myCircle = new MyroCircle(myCanvas, (int)xPos, (int)yPos, RADIUS); myCircle.setFillColor(new java.awt.Color(b,r,g)); myCircle.makeFilled(); } public void animate(double xVelocity, double yVelocity) { boolean notDone = true; double overBounce; while(notDone) { myCircle.setCenter((int)xPos, (int)yPos); myCircle.visible(); //add gravity to y velocity yVelocity += GRAVITY; //add the velocities to x and y xPos += xVelocity; yPos += yVelocity; if (Math.abs(xVelocity) < 1) xVelocity = 0.0; //right side overBounce = xPos - maxX + RADIUS; if (overBounce > 0) { xPos -= 2.0*overBounce; xVelocity *= -1.0; xVelocity *= ENERGYLOSS; yVelocity *= ENERGYLOSS; } //Left side overBounce = xPos - RADIUS; if (overBounce < 0) { xPos -= 2.0*overBounce; xVelocity *= -1.0; xVelocity *= ENERGYLOSS; yVelocity *= ENERGYLOSS; } //bottom overBounce = yPos - maxY + RADIUS; if(overBounce > 0) { yPos -= 2.0*overBounce; yVelocity *= -1.0; xVelocity *= ENERGYLOSS; yVelocity *= ENERGYLOSS; if (Math.abs(yVelocity) < 2) { yVelocity = 0.0; if (Math.abs(xVelocity) < 1) { notDone = false; } } } //sleep MyroUtils.sleep(DELAY); myCircle.invisible(); } } }