Thursday, March 5, 2009

A stopwatch timer

Of the three areas I need to research, a timer, dynamic graphics, and storing data, I shall begin by looking for a timer. I have seen reference to one but I can't remember where. I notice a link to search the tutorial in the top right hand corner of the page. I search "timers". I am led to a page on the Swing Timer, which begins by warning "Don't confuse Swing timers with the general-purpose timer facility that was added to the java.util package in release 1.3. This page describes only Swing timers." As it happens I am interested in "the general-purpose timer". It would be nice if there were a link on this text, but the is not, so I search "general-purpose timer", and guess what: I am led to the same text on the same page.

There is a link to the javax.swing.Timer) in the javax API so I follow that to look for clues. Certainly I can see where I might use it when I have my graphics going, but just now it is not what I want. So I reverse back into the API to look for the java.util.Timer. And this performs essentially the same function as the swing timer - it releases an event after a predefined interval. It's what mobile phones call a countdown timer. I'm looking for a stopwatch timer. So I search for stopwatch, but there are no matches.

In VB there is a timer object which can be made to work like a stopwatch. But an earlier incarnation of my app used a Mac tool called Hypercard. In that I used the system clock to measure the time elapsing between two events, simply by getting the system at the two events and measuring the difference.

When in doubt I go to this forum and I usually find an answer without posting a question. In this case it seems that I am not the only person in the world who has ever wanted to time anything but that other people seem to build their own timers. For my purposes, something along the following lines might work.

startTime = System.currentTimeMillis()
endTime = System.currentTimeMillis()
timeTaken =
endTime - startTime

It remains to be seen whether it actually does so.

No comments: