• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need a variable that increments by 5 every 5th second and retain the value for the whole of it

 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have made a swing application. It uses timer FOR performing certain tasks and it uses the following code get the current second:


Now, if the current second is completely divisible by 5, then a graph is updated.ie. graph gets updated every 5th second The graph should begin at time, t = 0; So, what i have done is initialised a variable 'time' to 0. When the second got from above code is divisible by 5, then graph is updated and time is incremented by 5.
Now, the graph is updated for the whole of the 5th second. Now, the problem i face is that the variable time is getting incemented by 5 twice in the 5th second...so, i am multiple values of x-axis (which corresponds to time) as the variable 'time' represents the x-axis, and hece, a wrong result.
I want the variable time to retain the same value for the whole of 5th second and get incremented by 5 in th enext 5th second.
Any idea?
Thanks!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about setting the timer to a 5-second interval?
Then you can run your process every time it wakes up.
Am I missing something?

Jim ... ...
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well. A whole task is perofrmed, like repainting of panels, continuously for 100seconds and the graph is plotted evyr 5th second. So i cant do that.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you doing that runs continuously for 100 seconds?
 
Rose Jac
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repainting of panels...actually, several circles and few images that are redrawn using timer, so as to create "motion".
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can't take the timings from that to increment your number? Not sure what else to suggest. Anybody else?
 
Stephan van Hulst
Bartender
Pie
Posts: 5608
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why can't you use a separate timer to plot the graph?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't do that, and your other Timer is on a rapid time interval, you could try this:

- keep track of the last time the graph was drawn (obtained from System.currentTimeMillis())

- every step, compare the current time to the last time

- if it's more than 5 seconds, redraw the graph and update the "last time" variable
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic