// Zorah Fung, CSE 142 // Creates an animation of a ball falling from gravity import java.awt.*; public class BallDemo { public static final double GRAVITY = 9.81; public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(400, 600); Graphics g = panel.getGraphics(); int initX = 100; int initY = 100; int initV = 24; int initX2 = 200; int initY2 = 50; int initV2 = 30; for (double t = 0; t < 10; t+= 0.05) { double dis = displacement(initV, GRAVITY, t); g.setColor(Color.RED); g.fillOval(initX, initY + (int) dis, 50, 50); double dis2 = displacement(initV2, GRAVITY, t); g.setColor(Color.BLUE); g.fillOval(initX2, initY2 + (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; } }