Results 1 to 7 of 7
  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Problems with animation smoothness

    Hello. The last weeks I am working on a board game with cards in Swing. I have completed the main game, everything goes ok.

    I have many threads running at once with no problem. When I play a card to the board, 4 threads run and complete.

    The problem started when I tried to run my program at another computer. The animation is too slow, way too slow. For example it takes around 500 ms to draw a card at my computer and at the other around 3 seconds. I run it then to other computers as well and, surprisingly, in all computers except mine, animations are slow. Yes, my computer is I7 but I didn't do any graphics to use that core power. So, the only thing I am thinking is that I did bad programming.

    Here is the core of my program. I have a class Card extends JButton and implementing an interface. I have each card at a specific coordination when the game starts. When I draw, play, discard or do other actions with the card, I execute Threads with executorService. I animate the buttons using button.setBounds(x, y, prefSize.width, prefSize.height). I set velocityX=1 and Y=1 for the button movements and Thread.sleep(1) for each thread to do the animation.

    So when I click on a card, 4 such threads start running, each one animating a different button component. I don't use paint or paintComponent, all I do is setBounds from the sub-classes Threads with sleep(1) or (2) to perform the animation.

    At my computer everything works perfect. But only at mine. Have I done anything wrong? Have I set on move too many threads? I used double buffer strategy but it didn't solve the problem.

    I was prompt to use JavaFX because I won't have such problems at all.

    Do you have any suggestions?

    Thank you.

    John

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: Problems with animation smoothness

    Yikes, why are you using so many threads to perform an animation? It's a huge no-no to update Swing components outside of the EDT. I'm surprised it worked at all. I'd recommend you use a Swing Timer instead- and you only need one, not four. Some refactoring might be in your future.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Problems with animation smoothness

    I am using that many thread cause each one of them does a specific thing and not all of them are needed each time.

    I play a card:

    Thread 1 - Puts it on board
    Thread 2 - Rearranging cards board
    Thread 3 - Rearranging cards hand (cause I may have chose to throw the second card for example, so all the others strafe accordingly to rearrange)
    Thread 4 - Draw new card.

    There are some times that more threads added and others that only some of them happen. So I decided to run each movement in its own thread and each time I want one, to call it.

    I use all of them at once because it buys me huge time and it decreases the time of waiting. At start, I programmed it such way so each animation runs alone and each game took around 4 minutes. I thought of changing it and now it only takes less than 2 which is a huge improvement.

    Is animating buttons so hard process being made with several threads? Would it be better if I use Swing Timer?

    I am very disappointed because my program runs exactly as I want to, but only in my computer. My main purpose was to have my game on me all the time and run it at every pc I use any given time.
    Last edited by Sutured; 10-10-2012 at 06:53 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: Problems with animation smoothness

    You can't update Swing from threads other than the EDT- it results in undefined behavior, which is exactly what you're seeing. I'm actually surprised it works on your computer in the first place. You're going to want to move all your code into a Swing Timer (which fires on the EDT) and use if statements to figure out what should be going on each time.

    You could maybe try using SwingUtilities.invokeAndWait(), but really, using a separate thread for each animation almost definitely isn't the way to go.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Problems with animation smoothness

    This is a major point you brought up and it might change all my program. I don't think that I need refactor but reimplement itself. So that is the reason for this behavior... That is sad, because I liked very much the thread-like system I had. It was clean, easy to read, very easy to synchronize when needed using many different ways.

    I don't know if I will take that step or just leave it as it is and start using JavaFX to create it.

    I have been said and I read at forums that Timeline class is very powerful and due to the FX structure I won't have such problems inplementing these things I want and almost at the same way. Seems like I will try to see it.

    Is JavaFX that strong indeed?

    Thank you Kevin for all this.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,690
    Rep Power
    8

    Default Re: Problems with animation smoothness

    I don't really know anything about JavaFX, sorry.

    One way to ease your refactoring might be to start out by converting each Thread into an individual Timer. That should take care of your issues with updating Swing, and from there you can think about compacting it into a single Timer (or not, up to you).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Problems with animation smoothness

    Yes, this. I will make each thread a swing timer and try to transform it and make it work.

    I will be back in a few days posting and discussing the results.

    Thank you again for all this and for the idea to process.

Similar Threads

  1. Rain animation help
    By Bestsanchez in forum AWT / Swing
    Replies: 2
    Last Post: 01-14-2012, 11:47 PM
  2. Animation
    By ryainad in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2011, 05:52 PM
  3. gui animation problems
    By helpisontheway in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 02:41 PM
  4. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  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
  •