import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class BouncingBallsExt extends Applet { private static int width; private static int height; private int diameter = 10; // Diameter of the balls private int radius = diameter/2; // Radius of the balls private Choice choice; private TextArea echoArea; private Button clearButton, newBallButton; private Color color; private String colorName; private int lastClickX; private int lastClickY; private int lastMotionX; private int lastMotionY; private int clickNumber; private Vector balls; public void init ( ) { setBackground(Color.white); height = getBounds().height; width = getBounds().width; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); balls = new Vector(); // Create a Clear button and add it to the Frame. clearButton = new Button("Clear"); clearButton.setForeground(Color.black); clearButton.setBackground(Color.lightGray); add(clearButton); clearButton.addActionListener(new ClearButtonHandler()); // Create a NewBall button and add it to the Frame. newBallButton = new Button("New Ball"); newBallButton.setForeground(Color.black); newBallButton.setBackground(Color.lightGray); add(newBallButton); newBallButton.addActionListener(new NewBallButtonHandler()); // Create a menu of user choices and add it to the Frame. choice = new Choice(); choice.addItem("Orange"); choice.addItem("Green"); choice.addItem("Magenta"); choice.addItem("Blue"); add(choice); choice.addItemListener(new ChoiceHandler()); // Create a TextArea and add it to the Frame. echoArea = new TextArea(2, 48); echoArea.setEditable(false); add(echoArea); } // init public void start ( ) { repaint(); newBallButton.setEnabled(true); clickNumber=0; color = Color.orange; colorName = "Orange"; choice.select("Orange"); echoArea.setText("Color: Orange"); echoArea.append("\nClick New Ball button to create a new ball with this color"); } // start public void stop () { for (int i=0; i= BouncingBallsExt.width) dx = - dx; if (y < 0 || y >= BouncingBallsExt.height) dy = - dy; x += dx; y += dy; } public void paint (Graphics g) { g.setColor(color); g.fillOval(x, y, diameter, diameter); } public synchronized void stopBall () { //substitutes stop() which is deprecated alive = false; } public synchronized boolean isLiving() { return alive; } public synchronized void pauseBall() { pause = true; } public synchronized void resumeBall() { pause = false; } public synchronized boolean isPaused() { return pause; } public void run () { while (isLiving()) { if (!isPaused()) { move(); if (this==balls.lastElement()) repaint(); } try { Thread.sleep(50); } catch (InterruptedException exc) { } } } } // Ball } // BouncingBalls