// Helene Martin, CSE 142 // Animates two falling balls affected by gravity. import java.awt.*; public class BallSimulation { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(400, 400); Graphics g = panel.getGraphics(); //System.out.print(displacement(3.0, 4.0, 5.0)); //returns 65.0 int ball1x = 20; int ball1y = 20; double ball1V0 = 30; int ball2x = 100; int ball2y = 10; double ball2V0 = 40; for(double t = 1; t <= 10; t += 0.1) { double ball1Disp = displacement(ball1V0, 9.81, t); g.setColor(Color.RED); g.fillOval(ball1x, (int)(ball1y + ball1Disp), 40, 40); double ball2Disp = displacement(ball2V0, 9.81, t); g.setColor(Color.BLUE); g.fillOval(ball2x, (int)(ball2y + ball2Disp), 40, 40); panel.sleep(100); panel.clear(); } } // Calculates 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) { return v0 * t + (a / 2) * Math.pow(t, 2); } }