// Zorah Fung, CSE 142 // Creates an animation of a ball falling from gravity import java.awt.*; import java.util.*; public class BallDemo { public static final double GRAVITY = 9.81; public static void main(String[] args) { Scanner console = new Scanner(System.in); int ballX = 100; int ballY = 100; System.out.print("Ball 1 Initial velocity? "); int ballV0 = console.nextInt(); int ball2X = 200; int ball2Y = 50; System.out.print("Ball 2 Initial velocity? "); int ball2V0 = console.nextInt(); DrawingPanel panel = new DrawingPanel(400, 600); Graphics g = panel.getGraphics(); for (double t = 0; t < 10; t+= 0.05) { double dis = displacement(ballV0, GRAVITY, t); g.setColor(Color.RED); g.fillOval(ballX, ballY + (int) dis, 50, 50); double dis2 = displacement(ball2V0, GRAVITY, t); g.setColor(Color.BLUE); g.fillOval(ball2X, ball2Y + (int) dis2, 50, 50); panel.sleep(50); panel.clear(); } } // Given an initial velocity and acceleration an object, returns the displacement // the object at time t public static double displacement(double v0, double a, double t) { return v0 * t + 0.5 * a * t * t; } }