// Allison Obourn, CSE 142 // Animates two falling balls affected by gravity. // prompts the user to get their x and y starting positions // and starting velocity import java.awt.*; import java.util.*; public class BallSimulation { public static void main(String[] args) { //System.out.println(displacement(15, 30, 3)); Scanner console = new Scanner(System.in); System.out.print("enter x, y for ball 1: "); // 42 42 int ball1x = console.nextInt(); int ball1y = console.nextInt(); System.out.print("enter velocity for ball 1: "); double ball1v0 = console.nextDouble(); System.out.print("enter x, y for ball 2: "); int ball2x = console.nextInt(); int ball2y = console.nextInt(); System.out.print("enter velocity for ball 2: "); double ball2v0 = console.nextDouble(); DrawingPanel p = new DrawingPanel(400, 400); Graphics g = p.getGraphics(); 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; } }