Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Animation pause button

Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
Posts: 3323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What timer are you using? - is it the one in the swing package or the util package.

I assume by that fact you have mentioned an actionPerformed() method it's the swing package Timer. This timer uses the EDT to run the actionPerformed code which calls repaint() which causes an event to be placed on the event queue to repaint the GUI. This event is run on the EDT which makes the call to paintComponent(). If this cycle takes longer than the timer's time delay then your GUI is going to become very unresponsive.

If the actionPerformed method does any time consuming computation then you may get a little benefit by switching to a java.util.Timer (any thread can call repaint()).
If you can reduce the time taken to redraw the panel eg by pre-compute some of the image drawing etc that will also help but whatever you do the GUI is going to become increasingly unresponsive as the redraw time approaches the time delay.

Can you show your paintComponent() method so we can see what it is doing.

A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic