// This program animates a ball moving at a particular angle and with // a particular velocity. import java.awt.*; import java.util.*; public class Ball2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); // get angle and velocity from the user System.out.println("Enter Velocity: "); double velocity = console.nextDouble(); System.out.println("Enter Angle: "); double angle = console.nextDouble(); // find the xVelocity and yVelocity based on the angle and velocity double xVelocity = velocity * Math.cos(Math.toRadians(angle)); double yVelocity = -velocity * Math.sin(Math.toRadians(angle)); int seconds = 100; // set up Drawing Panel DrawingPanel p = new DrawingPanel(600, 600); p.setBackground(Color.CYAN); Graphics g = p.getGraphics(); // draw initial ball double x = 300.0; double y = 300.0; g.fillOval((int) x - 5, (int) y - 5, 10, 10); //pause two seconds p.sleep(2000); for (int i = 1; i <= 10 * seconds; i++) { // cover old ball with white g.setColor(Color.WHITE); g.fillOval((int) x - 5, (int) y - 5, 10, 10); // move ball x = x + xVelocity / 10.0; y = y + yVelocity / 10.0; //bounce ball (change direction if at wall) if (x <= 5 || x >= 595) { xVelocity = -xVelocity; } if (y <= 5 || y >= 595) { yVelocity = -yVelocity; } // draw new ball g.setColor(Color.BLACK); g.fillOval((int) x - 5, (int) y - 5, 10, 10); //pause .1 seconds p.sleep(100); } } }