File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Need a variable that increments by 5 every 5th second and retain the value for the whole of it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need a variable that increments by 5 every 5th second and retain the value for the whole of it" Watch "Need a variable that increments by 5 every 5th second and retain the value for the whole of it" New topic
Author

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

Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
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

Joined: Oct 13, 2005
Posts: 43305
    
  32
What are you doing that runs continuously for 100 seconds?
Rose Jac
Ranch Hand

Joined: May 20, 2011
Posts: 33
Repainting of panels...actually, several circles and few images that are redrawn using timer, so as to create "motion".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43305
    
  32
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

Joined: Sep 20, 2010
Posts: 3884
    
  18

So why can't you use a separate timer to plot the graph?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4536
    
    8

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need a variable that increments by 5 every 5th second and retain the value for the whole of it
 
It's not a secret anymore!