//******************************************************************** // reboundpanel2.java Author: Pat Moss Rev. 02 11-11-2005 // // Represents the primary panel for the rebound program. // Includes Start/Stop animation by clicking the mouse. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class reboundpanel2 extends JPanel { private final int WIDTH = 300, HEIGHT = 100; private final int DELAY = 20, IMAGE_SIZE = 35; private ImageIcon image; private Timer timer; private String timerstate = "stop"; // Timer state is "stop" or "start" private int x, y, moveX, moveY; //----------------------------------------------------------------- // Sets up the panel, including the timer for the animation. // And the mouse listener to start/stop the animation. //----------------------------------------------------------------- public reboundpanel2() { timer = new Timer(DELAY, new reboundlistener()); linelistener listener = new linelistener(); addMouseListener (listener); addMouseMotionListener (listener); image = new ImageIcon ("happyface.gif"); x = 0; y = 40; moveX = moveY = 3; setPreferredSize (new Dimension(WIDTH, HEIGHT)); setBackground (Color.black); timer.start(); timerstate = "start"; } //----------------------------------------------------------------- // Draws the image in the current location. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); image.paintIcon (this, page, x, y); } //***************************************************************** // Represents the action listener for the timer. //***************************************************************** private class reboundlistener 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 >= WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y <= 0 || y >= HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); } } //***************************************************************** // Represents the listener for all mouse events. //***************************************************************** private class linelistener implements MouseListener, MouseMotionListener { //-------------------------------------------------------------- // Captures the initial position at which the mouse button is // pressed. //-------------------------------------------------------------- public void mousePressed (MouseEvent event) { if (timerstate == "stop") { timerstate = "start"; timer.start(); } else { timerstate = "stop"; timer.stop(); } } //-------------------------------------------------------------- // Provide empty definitions for unused mouse event methods. //-------------------------------------------------------------- public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} public void mouseDragged (MouseEvent event) {} } }