// Tyler Rigsby, CSE 142 // Does an animation of a ball falling down import java.awt.*; import java.util.*; public class BallDemo { public static void main(String[] args) { Scanner console = new Scanner(System.in); double ball1X = 150.0; double ball2X = 350.0; double ballY = 50.0; System.out.print("Enter first ball's initial velocity: "); double ball1V0 = console.nextDouble(); System.out.print("Enter first ball's acceleration: "); double ball1A = console.nextDouble(); System.out.print("Enter second ball's initial velocity: "); double ball2V0 = console.nextDouble(); System.out.print("Enter second ball's acceleration: "); double ball2A = console.nextDouble(); DrawingPanel panel = new DrawingPanel(500, 500); Graphics g = panel.getGraphics(); panel.setBackground(Color.ORANGE); for (double t = 0.0; t <= 20.0; t += .05) { double ball1Loc = ballY + displacement(ball1A, ball1V0, t); g.fillOval((int) ball1X, (int) ball1Loc, 30, 30); double ball2Loc = ballY + displacement(ball2A, ball2V0, t); g.fillOval((int) ball2X, (int) ball2Loc, 30, 30); panel.sleep(50); panel.clear(); } } // Returns the displacement of an object given acceleration a, // initial velocity v0, and time t public static double displacement(double a, double v0, double t) { return 0.5 * a * t * t + v0 * t; } }