java - Measure how long mouse was held for? -


i'm working on game involves holding on button. want able depending on how long button pressed display image ie:

x = seconds button held for

if 3.1 seconds > x > 2.9 seconds

then display image 1

if x < 2.9 or x > 3.1

then display image 2

how program using mouse listener?

thank you.

you can use below code snippet resolve problem -

double starttime, endtime, holdtime; boolean flag = false;  @override public final void mousepressed(final mouseevent e) {     starttime = system.nanotime();     flag = true; }  @override public final void mousereleased(final mouseevent e) {     if(flag) {         endtime = system.nanotime();         flag = false;     }     holdtime = (endtime - starttime) / math.pow(10,9); } 

the holdtime give time in seconds mouse tapped.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -