import java.awt.*; // Needed for window and graphics (explained in COMP1406) import javax.swing.*; // Needed for window and graphics (explained in COMP1406) // This application simulates balls bouncing in a window public class BallSimulation2 { // This variable stores the panel that displays the balls // (This is a topic discussed in COMP1406 course) public static BallPanel ballPanel; public static void startSimulation(Ball[] balls) { while(true) { for (int i=0; i= BallPanel.WIDTH) || (balls[i].x - balls[i].radius <= 0)) balls[i].direction = (float)(Math.PI - balls[i].direction); if ((balls[i].y + balls[i].radius >= BallPanel.HEIGHT) || (balls[i].y - balls[i].radius <= 0)) balls[i].direction = -balls[i].direction; } ballPanel.repaint(); try{ Thread.sleep(10); } catch(Exception e){}; } } // Create a window with a BallPanel, add 3 balls and start the simulation public static void main(String args[]) { Ball[] balls = new Ball[100]; for (int i=0; i<100; i++) { balls[i] = new Ball(); balls[i].x = 50 + (int)(Math.random() * 500); balls[i].y = 50 + (int)(Math.random() * 400); } JFrame frame = new JFrame("Ball Simulation"); frame.add(ballPanel = new BallPanel(balls)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); // Makes size according to panel's preference frame.setVisible(true); startSimulation(balls); } }