Results 1 to 20 of 21
- 05-04-2011, 06:00 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Trying to run side by side operations without interference
Okay, the title is obviously rather hard to understand, but I couldn't think of something more accurate. Now that that's out of the way, here's my problem:
I'm working on a game for java (in swing) for my final project (I'm in my first year of Programming) and I've run into a problem. I'm trying to make it so that I can basically perform multiple operations at once. Basically, I need a JButton to toggle between 2 colors every second without making it so that I can't do anything else, like click buttons. My best idea for delaying the color change would be Thread.sleep(), but doing that temporarily disables everything else. Suggestions?
- 05-04-2011, 07:52 PM #2
Make some timerclass extending Thread.
- 05-04-2011, 07:54 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 05-04-2011, 08:05 PM #4
Sorry Swallock, it is not my habbit to spoonfeed, but I want JOS to understand me.
Java Code:public class ButtonchangeTimer extends Thread { private ITimerTask task; private long sleepingTime = 1000;//default is a second private boolean pleaseStop = false; public ButtonchangeTimer(ITimerTask theTask){ task = theTask; } public void startAgain(){ pleaseStop = false; run(); } public void pause(){ pleaseStop = true; } public void run(){ while( !pleaseStop ){ doTask(); waitASec(); } } public void setSleepingTime( int mili ){ sleepingTime = mili; } private void waitASec(){ try{ Thread.sleep( sleepingTime ); }catch(InterruptedException e){} } private void doTask(){ task.doPerTick(); } }
- 05-04-2011, 08:18 PM #5
@Jos
Hey! I see i'm a senior member now. Is that when you reach the 100 posts?
Or am I revealing a topsecret?
- 05-04-2011, 08:21 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
To the OP, its better to read up and understand what the above code does...see the following
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
but take care to mix multithreading with single threaded GUI models (like AWT/Swing). See
How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
And in relation to the code Jodokus posted, I will argue that it's better practice to implement runnable and create a thread with that implementation rather than extend Thread
- 05-04-2011, 08:27 PM #7
@doWhile
Sorry, thread blocked. I'm reading........
-
My vote is to simply use a Swing Timer as linked to in doWhile's post above.
- 05-05-2011, 12:40 AM #9
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Thanks, doWhile, I got that swing timer to do exactly what I needed. Thanks for the link!
- 05-05-2011, 07:07 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-05-2011, 11:46 AM #11
Please don't be so harsh on me. I detailed my firsts post in the first place on your request. I once adapted some example and used it in some programs, for instance a clock, obediently ticking away (and never being stopped by me). You are right that using "run();" in startAgain() is not a good idea.That class doesn't make sense
doPerTick() is obviously a method implemented by ITimerTask task, moving my clock or changing OP's buttoncolors.
And of course, being largely an autodidact, there's always a chance of missing some supplied libraryfunction. That's one of my reasons of being here: see how others do it.Last edited by Jodokus; 05-05-2011 at 12:19 PM. Reason: spelling
-
- 05-05-2011, 03:49 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
-
- 05-05-2011, 04:26 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-05-2011, 05:28 PM #16
Wow, I obviously stirred up a long endured friendship.
"Harsh" may have been a too strong word. It's just because I appreciated Jos as friendly and forgiving. But now I can sleep the sleep of the innocent again. Thanks Fubarable.
- 05-05-2011, 07:07 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 05-05-2011, 07:22 PM #18
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Create a new thread.
-
- 05-06-2011, 07:09 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You start another thread because this one is ours; we are going to build a windmill on top of it; put a red light district somewhere in one of its corners and sell weed all over the place; and cheese and beer (Grolsch) and fine black tobacco. It'll be a fine place; this is going to be a world famous thread and I hereby declare me, myself to be the queen of this thread; so there.
kindest regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
how to countdown on server side
By BigBear in forum Java ServletReplies: 3Last Post: 04-26-2010, 10:33 PM -
Side-scrolling
By shadycharacter in forum New To JavaReplies: 0Last Post: 04-21-2010, 04:20 PM -
how to access Ms Access from client side to server side
By arunkumarinfo in forum JDBCReplies: 1Last Post: 03-20-2010, 07:03 PM -
Deploy Jar Side By Side
By Adrian in forum EclipseReplies: 0Last Post: 04-11-2008, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks