|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectevent.Metronome
public class Metronome
A Metronome "ticks" at regular intervals, informing any registered listeners
We use a Metronome instead of a java.util.Timer or a javax.swing.Timer object for two reasons:
The biggest disadvantage of this class is that it does not use thread sharing. Hence, an application should not use many different Metronome objects.
A Metronome may "drift" since the sleep() method is not guaranteed to return in exactly the right amount of time. A "fixed rate" metronome will attempt to fix this (by notifying listeners in quick succession).
The time that is reported to listeners will depend on the operating mode. A "fixed interval" Metronome will report the time as if it is drift-free. That is, the difference in reported times will be exactly the delay (even when the Metronome has drifted). A "fixed rate" Metronome will report the time as if it is correcting the drift. That is, the different in reported times will reflect the amount of time it tried to wait between reports. Neither will report the actual time between reports. A listener that wants to know this information must use System.currentTimeMillis() itself.
Field Summary | |
---|---|
protected int |
delay
|
protected int |
multiplier
|
protected int |
time
|
protected java.lang.Thread |
timerThread
|
Constructor Summary | |
---|---|
Metronome()
Default Constructor |
|
Metronome(int delay)
Explicit Value Constructor Constructs a "fixed interval" Metronome with the given delay |
|
Metronome(int delay,
boolean adjusting)
Explicit Value Constructor |
Method Summary | |
---|---|
void |
addListener(MetronomeListener ml)
Add a MetronomeListener |
int |
getDelay()
Get the current delay |
int |
getNumberOfListeners()
Get the number of listeners |
protected void |
notifyListeners()
Notify observers in the GUI/event-dispatch thread. |
void |
removeListener(MetronomeListener ml)
Remove a MetronomeListener |
void |
reset()
Reset the time Note: This method should only be called when the Metronome is not running |
void |
run()
The code that is executed in the timer thread (required by Runnable) |
void |
setMultiplier(int multiplier)
Set the multiplier (i.e., the apparent speed-up factor) Note: This method should only be called when the Metronome is not running |
void |
setTime(int time)
Set the current time Note: This method should only be called when the Metronome is not running |
void |
start()
Start this Metronome |
void |
stop()
Stop this Metronome "immediately" |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected volatile int delay
protected volatile int multiplier
protected volatile int time
protected java.lang.Thread timerThread
Constructor Detail |
---|
public Metronome()
public Metronome(int delay)
delay
- The number of milliseconds between tickspublic Metronome(int delay, boolean adjusting)
delay
- The number of milliseconds between ticksadjusting
- true for "fixed rate"; false for "fixed interval"Method Detail |
---|
public void addListener(MetronomeListener ml)
ml
- The MetronomeListener to addpublic int getDelay()
public int getNumberOfListeners()
protected void notifyListeners()
public void removeListener(MetronomeListener ml)
ml
- The MetronomeListener to removepublic void reset()
public void run()
run
in interface java.lang.Runnable
public void setMultiplier(int multiplier)
multiplier
- The multiplierpublic void setTime(int time)
time
- The current timepublic void start()
public void stop()
|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |