// Allison Obourn, CSE 142 // Animates two falling balls affected by gravity. import java.awt.*; public class BallSimulation { public static void main(String[] args) { //System.out.println(displacement(15, 30, 3)); DrawingPanel p = new DrawingPanel(400, 400); Graphics g = p.getGraphics(); int ball1x = 20; int ball1y = 20; double ball1v0 = 25; int ball2x = 100; int ball2y = 60; double ball2v0 = 15; for (double t = 1; t <= 10; t += 0.1) { double ball1disp = displacement (ball1v0, 9.81, t); g.setColor(Color.RED); g.fillOval(ball1x, ball1y + (int)ball1disp, 40, 40); double ball2disp = displacement (ball2v0, 9.81, t); g.setColor(Color.BLUE); g.fillOval(ball2x, ball2y + (int)ball2disp, 40, 40); p.sleep(100); p.clear(); } } // Returns the displacement of an objects starting at velocity v0 // after t seconds have passed, given an acceleration of a. public static double displacement(double v0, double a, double t) { double disp = v0 * t + 0.5 * a * Math.pow(t, 2); return disp; } }