CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2009

    Animation not stopping

    Someone told me that one (or more) redraw(s) is/are triggered even after I do timer.stop(). I just cannot see how and would really appreciate it if someone could bring it out in an obvious way.

    Anyways, here is the code:

    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    public class Bounds extends JPanel
        int x = 50, y = 50, width = 20, height = 20;
        Timer timer;
        public Bounds()
            JFrame frame = new JFrame();
            ActionListener listener = new graphicsListener();
            timer = new Timer(200, listener);
        protected void paintComponent(Graphics g)
            super.paintComponent(g); // so that the background gets refreshed
            g.drawRect(x, y, width, height);
            System.out.println("Panel width: " + getWidth() + " Panel height: " + getHeight());
            System.out.println("x+width = " + (x+width));
            System.out.println("y+height = " + (y+height));
            if(x+width >= getWidth() || y+height >= getHeight())
                System.out.println("Out of bounds!");
                JOptionPane.showMessageDialog(null, "Out of bounds!");
                timer.stop(); // Why isn't this having an effect?
        public class graphicsListener implements ActionListener
            public void actionPerformed(ActionEvent event)
        public static void main(String[] args)
            new Bounds();
    Any help would be greatly appreciated!
    Thanks in advance!

  2. #2
    Join Date
    May 2006

    Re: Animation not stopping

    javax.swing.Timer uses it's own thread to to perform the waiting for and triggering of events but the action event handlers execute on another thread the event-dispatching thread. This makes it easy to interact with the GUI as all GUI operations have to be done on the event-dispatching thread but it does mean there is a disconnection between the events been triggered and being handled.

    Therefore, if your timer triggers events quicker than the event-dispatching thread can process them they will be queued in the event-dispatch queue. When you stop the timer from running no more events will be added to the event-dispatch queue but any events still in the queue will be processed.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.