the following is the programming code,..i need to stop the rebound animation on a click of the mouse and continue its animation on another click
so far I could stop the animation by using timer.stop(), but its not starting back again!!..i cant understand why???
can anyone fix it for me
thanks
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 DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image;
private Timer timer;
private int x, y, moveX, moveY;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public ReboundPanel()
{
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon ("happyFace.gif");
addMouseListener(new ReboundListener());
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.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, MouseListener
{
//--------------------------------------------------------------
// 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();
}
public void mouseClicked (MouseEvent event)
{
int count=0;
count=event.getClickCount();
if(count%2==1)
timer.stop();
if(count%2==0)
{
timer.start();
repaint();
}
}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseDragged(MouseEvent event){}
public void mousePressed(MouseEvent event){}
public void mouseMoved(MouseEvent event){}
}
}