//***************************************************************
//  Rebound2.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 Rebound2 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();

        x1 = 0;
        y1 = 40;
        x2 = APPLET_WIDTH - 50;
        y2 = APPLET_HEIGHT - 50;
        moveX1 = moveY1 =  3;
        moveX2 = moveY2 = -3;

        image = getImage(getCodeBase(), "happyFace.gif");

        setBackground(Color.black);
        setSize(APPLET_WIDTH, APPLET_HEIGHT);
    }

    //------------------------------------------------------------
    //  Draws the image at the current (x1,y1) location.
    //------------------------------------------------------------
    public void paint(Graphics g) {
        g.drawImage(image, x1, y1, this);
        g.drawImage(image, x2, y2, 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 x1, y1, x2, y2;
    private int moveX1, moveY1, moveX2, moveY2;

    //************************************************************
    //  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) {
            x1 += moveX1;
            y1 += moveY1;

            if (x1 <= 0 || x1 >= APPLET_WIDTH - IMAGE_SIZE)
                moveX1 = -moveX1;

            if (y1 <= 0 || y1 >= APPLET_HEIGHT - IMAGE_SIZE)
                moveY1 = -moveY1;

            x2 += moveX2;
            y2 += moveY2;

            if (x2 <= 0 || x2 >= APPLET_WIDTH - IMAGE_SIZE)
                moveX2 = -moveX2;

            if (y2 <= 0 || y2 >= APPLET_HEIGHT - IMAGE_SIZE)
                moveY2 = -moveY2;

            repaint();
        }
    }

} // end class Rebound
