Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2010, 01:52 AM
Member
 
Join Date: Feb 2010
Posts: 21
Rep Power: 0
martypapa is on a distinguished road
Default Listening for variables?
Hi,
At the moment if I want to detect a change in a variable, I use a thread to loop, and then check if the variable is different from itself one loop before. I'm sure this isn't the most efficient way to do this though.

Is it possible to listen for variable changes like you can listen for mouse clicks?
Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-09-2010, 02:15 AM
Senior Member
 
Join Date: Nov 2009
Posts: 229
Rep Power: 1
collin389 is on a distinguished road
Default
Quote:
Is it possible to listen for variable changes like you can listen for mouse clicks?
Thanks.
I'm not quite sure what you mean by this, but you could start a separate thread that run a while loop checking if the variable is equal to it's instance of the variable. so if one changes, the loop will replace its variable with the real one and run a method you want to run.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-09-2010, 02:19 AM
Member
 
Join Date: Feb 2010
Posts: 21
Rep Power: 0
martypapa is on a distinguished road
Default
Hmmm that's similar to what I'm already doing, but I guess that's the way I'll do it then.
Thank you!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-09-2010, 02:44 AM
Member
 
Join Date: Feb 2010
Posts: 21
Rep Power: 0
martypapa is on a distinguished road
Default
Say if I had 20 buttons, would it be alright to use a thread for each of them? Do they take up much of the systems resources, (are there a limited number of threads I can use?)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-09-2010, 05:17 AM
Senior Member
 
Join Date: Nov 2009
Posts: 229
Rep Power: 1
collin389 is on a distinguished road
Default
why not just have 1 thread that uses a while(true) loop and an if statement for each button?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-09-2010, 05:41 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 8,431
Rep Power: 11
Fubarable is on a distinguished road
Default
Rather than talk about a specific tactic of "listening to a variable", why not tell us your overall strategy here. Just what are you trying to achieve by this? Tell us this, and I'll bet we'll find a better way to solve it.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-09-2010, 09:14 AM
Member
 
Join Date: Feb 2010
Posts: 21
Rep Power: 0
martypapa is on a distinguished road
Default
It's alright now, I've fixed it. I thought that testing lots of variables from my main JPanel in a loop was drastically slowing down my game. I went around in a circle and ended up back where I started - testing for variables in my main JPanel. It wasn't this that was slowing down my game, it was the screen fader.

I had a large black rectangle who's alpha value decreased and was constantly being repainted. I fixed this up, so that it was only repainted if the alpha value changed. Now this went from chewing up 100% of my CPU to just 7%.

I guess repainting takes up a heap more of the processor power than I thought. I would have thought though, that because this is a simple 2D game that it wouldn't take up so much.

Am I doing something wrong, or is this just a limitation of Java?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MDB listening nipunalk Advanced Java 5 01-20-2010 05:10 AM
NIO Server: listening on client's side socket carek Networking 1 12-24-2009 07:30 AM
Listening to SMS, which is send without any port number uskris CLDC and MIDP 7 11-25-2009 11:34 PM
Java server listening on given interface? Joe2003 Advanced Java 2 01-26-2009 05:14 PM
Listening to a Printer Mosd New To Java 0 08-12-2008 12:21 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 05:31 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org