0

I have a panel where a small red circle is shown and after some time the red circle is replaced by a yellow circle and the user has to click in the panel as quickly as possible when seeing the yellow circle. So the reaction time is the time in milliseconds between the moment the yellow circle is shown and the moment the user clicks in the large panel. This test is repeated 5 times.

How can I calculate the reaction time using javax.swing.Timer using System.currentTimeMillis() for the start of timer and the average reaction time of the 5 tests? (its required to use this)

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
Laynie_x
  • 23
  • 7

2 Answers2

1

to calculate execution time its advisable to use System.nanotime() instead of System.currentTimeMillis() See this link for more info

public static long nanoTime()

Returns the current value of the most precise available system timer, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Okay! The info on the link did help out. Thank you very much for the advice – Laynie_x Oct 12 '13 at 09:45
  • @Laynie_x accepting answer is the Stackoverflow of saying thank you – SpringLearner Oct 12 '13 at 09:46
  • Oh, doi! Sorry, I'm a real noobie here. Follow up Q, can I use 'long timeStamp = System.currentTimeMillis(); long reactionTime = System.nano() - timeStamp;' ? (sorry cant figure it out how to put it in code format here :( ) – Laynie_x Oct 12 '13 at 09:47
  • For code format within a line, use backquote. But in answer to your question, you can't do that. You can't mix using `currentTimeMillis()` and using `nanoTime()`. As the answer here says, "This method ... is not related to any other notion of system or wall-clock time". So you have to use `nanoTime()` at the beginning and the end of your interval. – Dawood ibn Kareem Oct 12 '13 at 20:20
1

Timer is a wrong tool for your requirement. Use it to change the color of the circle and to save the timestamp when you did it. Then have a click listener on the circle compare the saved timestamp with the current timestamp.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436