Results 1 to 2 of 2
Thread: MouseClicked Stop Animation
- 04-14-2009, 08:51 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 7
- Rep Power
- 0
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); } } }); } } }
- 04-14-2009, 09:38 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Similar Threads
-
Animation with Animated GIF
By JavaBean in forum Java 2DReplies: 3Last Post: 06-04-2011, 04:26 PM -
Need help with drawing multiple objects with mouseClicked method.
By busdude in forum New To JavaReplies: 6Last Post: 04-05-2009, 11:28 PM -
Can you stop a gif? xd
By Exhonour in forum New To JavaReplies: 0Last Post: 01-16-2009, 08:44 PM -
Text animation
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:44 PM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks