Hi,
I am very new to animation in Swing, and I'm having a problem understanding the best way to implement a pause button.
I have implemented a class which extends JPanel and which uses a Timer to do some animation. When the Timer calls actionPerformed(), a call to repaint() is made. In the paintComponent() method I am simply drawing random shapes which change each time, so the whole panel animates.
I have placed an instance of the animated JPanel into a JFrame, alongside a JButton which can be used to pause the animation. The pause button has an ActionListener registered to it - the actionPerformed() method of which will call a pause() method on the animation panel which looks like this:
This works fine...unless the animation is running with a very fast small ms delay. When this happens the pause button will not respond all the time - the faster the animation runs, the less chance you have to actually get the pause button to do anything. Am I correct to presume this is to do with both the animation repainting and the pause action running on the EDT, so the user never gets enough time to actually cause the button's actionPerformed() to be called?
I have read about the concept of the worker thread, which seems like something I might use for the animation so that the pause button will be responsive, but I don't understand how to get a non EDT thread to actually drive the animation - i.e. how can I get another thread to repaint() the panel? Or is there a proper way to do this with the Timer?
Does anyone have any pointers, or links to tutorials which might set me off on the right track please?