// program to make a ball bounce around a DrawingPanel // using if/else and user input (Scanner) import java.awt.*; import java.util.*; public class BouncingBall { public static void main(String[] args) { Scanner console = new Scanner(System.in); DrawingPanel panel = new DrawingPanel(600, 600); panel.setBackground(Color.CYAN); Graphics brush = panel.getGraphics(); double x = 300, y = 300; System.out.print("Enter velocity (pixels per second): "); double velocity = console.nextDouble(); System.out.print("Enter angle (degrees): "); double angle = console.nextDouble(); System.out.print("Enter time (seconds): "); int seconds = console.nextInt(); double xVel = velocity * Math.cos(Math.toRadians(angle)); double yVel = -velocity * Math.sin(Math.toRadians(angle)); brush.setColor(Color.RED); brush.fillOval((int)(x - 5), (int)(y - 5), 10, 10); panel.sleep(1000); for (int i = 0; i < 10*seconds; i++) { // turn old ball white brush.setColor(Color.WHITE); brush.fillOval((int)(x - 5), (int)(y - 5), 10, 10); // move ball x += xVel / 10; y += yVel / 10; // detect a bounce if (x <= 0 || x >= 600) { xVel = -xVel; } if (y <= 0 || y >= 600) { yVel = -yVel; } // redraw ball brush.setColor(Color.RED); brush.fillOval((int)(x - 5), (int)(y - 5), 10, 10); panel.sleep(100); } } }