hi
This code was written using the API library. I am only in my second year and would like some help using jdk equivalents to pause, Goval to add and remove a shape and a random colour generator. here is the java file, it requires some tweaking. acm.jar has been attached in the zip so you can see what Ive done so far. However Acm is now disfunct.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bouncingball;
import acm.graphics.*;
import acm.program.*;
import acm.util.RandomGenerator;
import java.awt.Color;
import java.awt.event.*;
/**
*
* @author Kuser
*/
public class BouncingBall extends GraphicsProgram implements Runnable
{
private static final int DIAM_BALL = 30;
private static final double GRAVITY = 3;
private static final int DELAY = 50;
private static final double X_START = DIAM_BALL/2;
private static final double Y_START = 360;
private static final double X_VEL = 5;
private static final double BOUNCE_REDUCE = 0.85;
private double xVel = X_VEL;
private double yVel = 0.0;
private GOval ball;
/**
* @param args the command line arguments
*/
public void run()
{
// TODO code application logic here
addMouseListeners();
setup();
while (ball.getX() < getWidth())
{
moveBall();
checkForCollision();
pause(DELAY);
}
}
public void mouseClicked(MouseEvent e)
{
RandomGenerator randomColor = new RandomGenerator();
ball.setColor(Color.white);
ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
ball.setFilled(true);
ball.setColor(randomColor.nextColor());
add(ball, e.getX(), e.getY());
}
public void setup()
{
ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);
ball.setFilled(true);
ball.setColor(Color.magenta);
add(ball);
}
public void moveBall()
{
yVel += GRAVITY;
ball.move(xVel, yVel);
}
public void checkForCollision()
{
if (ball.getY() > getHeight() - DIAM_BALL)
{
yVel = -yVel * BOUNCE_REDUCE;
double diff = ball.getY() - (getHeight() - DIAM_BALL);
ball.move(0, -2*diff);
}
}
}