/*********************************************************************** * Tom Kelliher * CS 245 * Bounce1.java * * This applet is a first pass at controlling a bouncing ball via a * moded start/stop button. * * Question to be answered: Explain precisely why the applet never * responds to clicks on the stop button. ***********************************************************************/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Bounce1 extends Applet implements ActionListener { private Canvas display = new Canvas(); // Ball rendered here. private Button startStop = new Button("Start"); private Ball ball = null; public void init() { setLayout(new BorderLayout()); add("South", startStop); add("Center", display); startStop.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (startStop.getLabel().equals("Start")) { startStop.setLabel("Stop"); // Change button mode. ball = new Ball(display, Color.red); // Render the ball. ball.draw(Color.black); //Continue moving and rendering the ball. while (startStop.getLabel().equals("Stop")) ball.move(); } else // Note: we are no longer rendering the ball. startStop.setLabel("Start"); } } /*********************************************************************** * class Ball --- implements a bouncing ball. ***********************************************************************/ class Ball { private Canvas theCanvas; // Reference to our drawing surface. private Color color; // Color of this ball. private int x, y; // Position of this ball. private double velocity; // X-velocity of this ball. final static int DELAY = 1000000; // Spin-loop delay factor. /*********************************************************************** * public Ball(Canvas c, Color co) --- c is the drawing surface, co is * the color in which to render the ball. ***********************************************************************/ public Ball(Canvas c, Color co) { theCanvas = c; color = co; x = c.getWidth() / 2; // Set random y and x-velocity factors. y = (int) (10 + (c.getHeight() - 20) * Math.random()); velocity = 0.6 + Math.round(2.5 * Math.random()); } /*********************************************************************** * public void draw(Color c) --- Render a 10 pixel ball as its current * position in color c. ***********************************************************************/ public void draw(Color c) { Graphics g = theCanvas.getGraphics(); g.setColor(c); g.fillOval(x, y, 10, 10); g.dispose(); } /*********************************************************************** * public void move() --- Paint over the ball in its former location, * update to its new position, and render. ***********************************************************************/ public void move() { // Delay in spin-loop. for (int i = 0; i < DELAY; ++i) ; // Erase former position. draw(Color.white); if (x <= 0 || x >= theCanvas.getWidth()) velocity = -velocity; // Update x coordinate. x = (int) (Math.round(x + velocity)); // Render ball at new position. draw(color); } }