Results 1 to 9 of 9
Thread: When do I use SwingWorker?
- 10-20-2011, 07:54 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 42
- Rep Power
- 0
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.
- 10-20-2011, 09:15 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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.
- 10-21-2011, 02:34 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 42
- Rep Power
- 0
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.
- 10-21-2011, 04:39 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
- 10-21-2011, 05:10 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 42
- Rep Power
- 0
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.
- 10-22-2011, 06:37 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 42
- Rep Power
- 0
Re: When do I use SwingWorker?
still need help with this
- 10-23-2011, 09:09 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 42
- Rep Power
- 0
Re: When do I use SwingWorker?
Still need help...
-
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.
- 10-24-2011, 12:40 AM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
SwingWorker
By 3.14.TR in forum Threads and SynchronizationReplies: 3Last Post: 03-14-2011, 04:53 PM -
How to stop SwingWorker?
By JStarter in forum AWT / SwingReplies: 14Last Post: 07-20-2010, 04:36 PM -
Using SwingWorker
By viking90 in forum New To JavaReplies: 1Last Post: 04-24-2010, 09:17 AM -
SwingWorker Opinions
By frejon26 in forum AWT / SwingReplies: 3Last Post: 04-13-2009, 08:41 PM -
swingworker
By musiigedeo in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 12:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks