//******************************************************************** // ReboundPanel.java Author: Lewis/Loftus // // Represents the primary panel for the Rebound applet. //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ReboundPanel extends JPanel { private final int WIDTH = 300, HEIGHT = 100; private final int IMAGE_SIZE = 35; private ImageIcon image; private Timer timer; private int x, y, moveX, moveY; //----------------------------------------------------------------- // Sets up the applet, including the timer for the animation. //----------------------------------------------------------------- public ReboundPanel (Timer countdown) { timer = countdown; timer.addActionListener (new ReboundListener()); image = new ImageIcon ("happyFace.gif"); x = 0; y = 40; moveX = moveY = 3; setBackground (Color.black); setPreferredSize (new Dimension(WIDTH, HEIGHT)); } //----------------------------------------------------------------- // 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(); } } }