//***************************************************************
//  Rebound.java         Authors: Lewis and Loftus
//                       Updated: Bary W Pollack
//
//  Demonstrates an animation and the use of the Timer class.
//***************************************************************

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class Rebound extends Applet {

    //------------------------------------------------------------
    //  Sets up the applet, including the timer for the animation.
    //------------------------------------------------------------
    public void init() {
        addMouseListener(new ReboundMouseListener());

        timer = new Timer(DELAY, new ReboundActionListener());
        timer.start();

        x = 0;
        y = 40;
        moveX = moveY = 3;

        image = getImage(getCodeBase(), "happyFace.gif");

        setBackground(Color.black);
        setSize(APPLET_WIDTH, APPLET_HEIGHT);
    }

    //------------------------------------------------------------
    //  Draws the image at the current (x,y) location.
    //------------------------------------------------------------
    public void paint(Graphics g) {
        g.drawImage(image, x, y, this);
    }

    private final int APPLET_WIDTH  = 200;
    private final int APPLET_HEIGHT = 100;

    private final int IMAGE_SIZE = 35;
    private final int DELAY = 20;

    private Timer timer;
    private Image image;
    private int x, y, moveX, moveY;

    //************************************************************
    //  Implements the mouse listener for the applet.
    //************************************************************
    private class ReboundMouseListener implements MouseListener {

        //---------------------------------------------------------
        //  Stops or starts the timer (and therefore the animation)
        //  when the mouse button is clicked.
        //---------------------------------------------------------
        public void mouseClicked(MouseEvent event) {
            if (timer.isRunning())
                timer.stop();
            else
                timer.start();
        }

        //---------------------------------------------------------
        //  Provide empty definitions for unused event methods.
        //---------------------------------------------------------
        public void mouseEntered(MouseEvent event) {}
        public void mouseExited(MouseEvent event) {}
        public void mousePressed(MouseEvent event) {}
        public void mouseReleased(MouseEvent event) {}
    }

    //*************************************************************
    //  Implements the action listener for the Timer.
    //*************************************************************
    private class ReboundActionListener implements ActionListener {

        //---------------------------------------------------------
        //  Updates the position of the image and possibly the direction
        //  of movement whenever the timer fires an action event.
        //---------------------------------------------------------
        public void actionPerformed(ActionEvent event) {
            x += moveX;
            y += moveY;

            if (x <= 0 || x >= APPLET_WIDTH - IMAGE_SIZE)
                moveX = -moveX;

            if (y <= 0 || y >= APPLET_HEIGHT - IMAGE_SIZE)
                moveY = -moveY;

            repaint();
        }
    }

} // end class Rebound
