// This program computes the trajectory of a projectile. import java.awt.*; public class Projectile { // constant for Earth's gravity acceleration in meters/second^2 public static final double ACCELERATION = -9.81; public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(420, 220); Graphics g = panel.getGraphics(); table(g, 60, 50, 10); g.setColor(Color.RED); table(g, 50, 80, 50); } // prints a table showing the trajectory of an object given // its initial velocity v and angle and number of steps public static void table(Graphics g, double v0, double angle, int steps) { double radians = Math.toRadians(angle); double v0x = v0 * Math.cos(radians); double v0y = v0 * Math.sin(radians); double totalTime = -2.0 * v0y / ACCELERATION; double dt = totalTime / steps; System.out.println(" step x y time"); for (int i = 0; i <= steps; i++) { double time = i * dt; double x = displacement(v0x, time, 0.0); double y = displacement(v0y, time, ACCELERATION); System.out.printf("%8d%8.2f%8.2f%8.2f\n", i, x, y, time); g.fillOval((int) x, (int) (220 - y), 5, 5); } } // returns the horizontal displacement for a body public static double displacement(double v, double t, double a) { return v * t + 0.5 * a * t * t; } }