Results 1 to 2 of 2
  1. #1
    xpngamer is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default MouseClicked Stop Animation

    I'm trying to have the animation stop when the mouse button is clicked and start again once the mouse button is clicked again. But when I run it it stops with the first click and then will only start again if the mouse button is held down or it will start then immediately stop. Any help would be appreciated.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ReboundPanelMouse 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;
    
       public ReboundPanelMouse()
       {
          timer = new Timer(DELAY, new ReboundListener());
    	  addMouseListener (new StopListener());
          image = new ImageIcon ("happyFace.gif");
    
          x = 0;
          y = 40;
          moveX = moveY = 3;
    
          setPreferredSize (new Dimension(WIDTH, HEIGHT));
          setBackground (Color.black);
          timer.start();
       }
    
    
       public void paintComponent (Graphics page)
       {
          super.paintComponent (page);
          image.paintIcon (this, page, x, y);
       }
    
       
       private class ReboundListener implements ActionListener
       {
         
          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 class StopListener extends MouseAdapter
       {
       	
      	//  Represents the action listener for the timer.
       	
       	public void mouseClicked (MouseEvent event)
       	{
    
    			if (event.getButton() == MouseEvent.BUTTON1)
    			{
    				timer.stop();
    			}
    			addMouseListener(new MouseAdapter() {
    				public void mousePressed(MouseEvent evt) {
    					if (evt.getButton() == MouseEvent.BUTTON1)
    					{
    						timer.start();
    						removeMouseListener(this);
    					}
    				}
    			});
       	}
    
       }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  2. Replies: 6
    Last Post: 04-05-2009, 11:28 PM
  3. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 08:44 PM
  4. Text animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:44 PM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •