Results 1 to 9 of 9
  1. #1
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default When do I use SwingWorker?

    I am programming a game loop for my, well, game. Duh. I was originally using threads to control this, but soon discovered that Swing is not thread safe. I was lead to SwingWorker, which does whatever task in the background and then updates it on the EDT. This would be great for something like a progress bar, UI, text etc. I think.

    The question is, how would I make the game loop? Should I use a swing timer for the game loop, and then not use SwingWorker at all for updating the actual game? Should I use a SwingWorker that repeats tasking the doInBackground method, and then update with the publish method? I have done some research, and it appears that a swing timer would be the best option, but I also saw in a couple of places to always use swing timers for the game loop.

    Thanks for any help.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: When do I use SwingWorker?

    A SwingTimer facilitates updating after a certain time period (either once or periodically), so if this is the behavior you need then its appropriate. For what its worth, you can update Swing from a Thread using SwingUtilities methods.

  3. #3
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: When do I use SwingWorker?

    Thanks! About the SwingUtilities... You say "For what it's worth". Does that mean that using SwingUtilities for this isn't standard, but still works? Or is it something I should look into? Being able to use Swing with threads without breaking any "rules" would be very... useful.

    Thanks again.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: When do I use SwingWorker?

    Quote Originally Posted by TacoManStan View Post
    Thanks! About the SwingUtilities... You say "For what it's worth". Does that mean that using SwingUtilities for this isn't standard, but still works? Or is it something I should look into? Being able to use Swing with threads without breaking any "rules" would be very... useful.

    Thanks again.
    It means that is a way to update Swing from a Thread, nothing more. SwingWorker was introduced when 1.6 was released, prior to that one had to use on SwingUtilities. Why not try both to see which one fits your task?

  5. #5
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: When do I use SwingWorker?

    Ok, thanks again. I, think I know what I should do, but I am not sure. Everything I am using is with arrays of pixel data, retrieved from a sprite sheet. This data is then added to the correct section of a BufferedImage covering an entire JPanel (the game itself). I was going to make a game loop with a SwingTimer, and then have some threads to calculate the positioning of the pixels, etc. I just don't know where to put them... I don't want to do this wrong. I made the mistake of being to "adventurous" before, and had to re-write everything. (What I am doing now).

    So yeah, if you have any tips, that would be great. If it would help, it is a 2D RPG style game based completely off tiles. I truly did try and find this by myself, so if you could point me to a couple links that would explain how to implement game loops with swing instead, that would be great.

    Thanks.

  6. #6
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: When do I use SwingWorker?

    still need help with this

  7. #7
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Posts
    50
    Rep Power
    0

    Default Re: When do I use SwingWorker?

    Still need help...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: When do I use SwingWorker?

    I'm not sure what your specific question is. Where specifically are you stuck? And usually the more specific the question, the more helpful the answer can be.

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: When do I use SwingWorker?

    You use SwingWorker when you want your GUI to have a thread of its own and not be interrupted, since if you use only Threads then your GUI will become unresponsive at times.

Similar Threads

  1. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 04:53 PM
  2. How to stop SwingWorker?
    By JStarter in forum AWT / Swing
    Replies: 14
    Last Post: 07-20-2010, 04:36 PM
  3. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 09:17 AM
  4. SwingWorker Opinions
    By frejon26 in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2009, 08:41 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 12:59 PM

Posting Permissions

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