public class Metronome
extends java.lang.Object
implements java.lang.Runnable
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.
Constructor and Description |
---|
Metronome()
Default Constructor.
|
Metronome(int delay)
Explicit Value Constructor.
|
Metronome(int delay,
boolean adjusting)
Explicit Value Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(MetronomeListener ml)
Add a MetronomeListener.
|
int |
getDelay()
Get the current delay.
|
int |
getNumberOfListeners()
Get the number of listeners.
|
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).
|
void |
setTime(int time)
Set the current time.
|
void |
start()
Start this Metronome.
|
void |
stop()
Stop this Metronome "immediately".
|
public Metronome()
public Metronome(int delay)
Constructs a "fixed interval" Metronome with the given 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"public void addListener(MetronomeListener ml)
ml
- The MetronomeListener to addpublic int getDelay()
public int getNumberOfListeners()
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)
Note: This method should only be called when the Metronome is not running
multiplier
- The multiplierpublic void setTime(int time)
Note: This method should only be called when the Metronome is not running
time
- The current timepublic void start()
public void stop()