/***********************************************************************
 * Bounce1.java
 *
 * This applet is a first pass at controlling a bouncing ball via a
 * moded start/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 drawn 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);

            // Draw the ball.

            ball.draw(Color.black);

            //Continue moving and drawing the ball.

            while (startStop.getLabel().equals("Stop"))
                ball.move();
        }
        else
            // Note: we are no longer drawing 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 int dx, dy;        // velocity of this ball.
    final static int DELAY = 10000000;  // Spin-loop delay factor.
    final static int RADIUS = 5;

    
/***********************************************************************
 * 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 = 0;
        y = (int) (10 + 480 * Math.random());
        dy = (int)(1.0 + Math.round(2.5 * Math.random()));
        dx = (int)(1.0 + 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, 2 * RADIUS, 2 * RADIUS);
        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);

        // Update coordinates.
        x += dx;  
        y += dy;
        
        // Check to see if ball bounces off a wall
        Dimension d = theCanvas.getSize();
        if (x < 0) {x = 0; dx = -dx;}
        if (x + 2*RADIUS >= d.width) { x = d.width-2*RADIUS; dx = -dx;}
        if (y < 0) {y = 0; dy = -dy;}
        if (y + 2*RADIUS >= d.height) { y = d.height-2*RADIUS; dy = -dy;}

        // Draw ball at new position.
        draw(color);
    }
}
