//******************************************************************** // Rebound.java Author: Lewis and Loftus // // 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 { 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; //----------------------------------------------------------------- // 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 in the current location. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawImage (image, x, y, this); } //***************************************************************** // Represents the mouse listner 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) {} } //***************************************************************** // Represents 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 * -1; if (y <= 0 || y >= APPLET_HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); } } }