EggTimer
Class EggTimer

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Window
                    |
                    +--javax.swing.JWindow
                          |
                          +--EggTimer.EggTimer
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, javax.swing.RootPaneContainer, java.io.Serializable

public class EggTimer
extends javax.swing.JWindow

A visible count-down timer. The hand of the watch makes exactly one complete revolution during the period for which the timer is set. The animation stops when the timer expires, or is stopped, but time continues to accumulate. The timer begins counting down only when it is "started". It can only be started once. After starting, the timer is in the "running" state until the time expires (or until it is stopped). In the running state, the timer can be paused and resumed (as many times as desired). While paused, no time is charged. The timer can be stopped only once; after stopping, no other state changes can happen. The percent time used of a stopped or expired timer is always 1.0. Ticks generate EggTimerChangeEvents. The timer raises an EggTimerRunoutEvent when the time expires. Clients can get some basic information about the timer status. It is NOT necessary for clients to implement the listener interface if they don't care about the event notifications; the visual timer is unaffected.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JWindow
javax.swing.JWindow.AccessibleJWindow
 
Nested classes inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static double defaultTimeIntervalInSeconds
          Length of time for which the timer runs, in seconds, by default.
static int defaultWindowWidth
           
static java.awt.Color windowBackgroundColor
           
 
Fields inherited from class javax.swing.JWindow
accessibleContext, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
EggTimer()
          Create and display an egg timer with a default number of seconds.
EggTimer(double seconds)
          Create and display an egg timer with the requested number of seconds before expiring.
 
Method Summary
 void addEggTimerListener(EggTimerListener listener)
          Add an EggTimerEvent listener
 double getElapsedTime()
          Answer, how much time has the timer accumulated since being started?
 double getOriginallyRequestedTime()
          Answer, how much time was originally requested for this timer?
 double getPercentUsed()
          Answer, what percentage of the requested time has elapsed?
 double getTimeRemaining()
          Answer the amount of time remaining before expiration.
 boolean isPaused()
          Answer, is the timer currently paused?
 boolean isRunning()
          Answer, is the timer running?
 boolean isStarted()
          Answer, has the timer been started?
 boolean isStopped()
          Answer, has the timer been stopped?
static void main(java.lang.String[] args)
          Just a little test method.
 boolean moveBy(int dx, int dy)
          Move the visible timer by the given amount (pixels).
 void pauseEggTimer()
          Freezes the time; elapsed time won't increase until timer is started again.
 void processEvent(java.awt.AWTEvent event)
          Override the JCompoment's processEvent method.
 void removeEggTimerListener(EggTimerListener listener)
          Remove the givien EggTimerListener
 void resumeEggTimer()
          Resumes an egg timer; elapsed time continues where it left off.
 void startEggTimer()
          Start the timer.
 void stopEggTimer()
          Stop an egg timer, forever.
 java.lang.String toString()
          Returns a String describing the egg timer.
 
Methods inherited from class javax.swing.JWindow
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, remove, setContentPane, setGlassPane, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update, windowInit
 
Methods inherited from class java.awt.Window
addNotify, addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, finalize, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getMostRecentFocusOwner, getOwnedWindows, getOwner, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindowStateListeners, hide, isActive, isFocusableWindow, isFocusCycleRoot, isFocused, isShowing, pack, postEvent, processWindowEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationRelativeTo, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, removeNotify, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, size, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultTimeIntervalInSeconds

public static final double defaultTimeIntervalInSeconds
Length of time for which the timer runs, in seconds, by default.

See Also:
Constant Field Values

defaultWindowWidth

public static int defaultWindowWidth

windowBackgroundColor

public static java.awt.Color windowBackgroundColor
Constructor Detail

EggTimer

public EggTimer()
Create and display an egg timer with a default number of seconds.


EggTimer

public EggTimer(double seconds)
Create and display an egg timer with the requested number of seconds before expiring.

Method Detail

startEggTimer

public void startEggTimer()
Start the timer.

Throws:
java.lang.RuntimeException - if the timer is already running.

pauseEggTimer

public void pauseEggTimer()
Freezes the time; elapsed time won't increase until timer is started again.

Throws:
java.lang.RuntimeException - if timer is already paused or in some other unpausible state.

resumeEggTimer

public void resumeEggTimer()
Resumes an egg timer; elapsed time continues where it left off.

Throws:
java.lang.RuntimeException - if time was not paused.

stopEggTimer

public void stopEggTimer()
Stop an egg timer, forever. Can only be called once.

Throws:
RuntimeExcetpion - if timer has not be started or is already stopped.

isRunning

public boolean isRunning()
Answer, is the timer running?

Returns:
true iff and only if the timer is currently running.

isPaused

public boolean isPaused()
Answer, is the timer currently paused?


isStarted

public boolean isStarted()
Answer, has the timer been started?


isStopped

public boolean isStopped()
Answer, has the timer been stopped?


getElapsedTime

public double getElapsedTime()
Answer, how much time has the timer accumulated since being started?

Returns:
the time elapsed in seconds; time during pauses is not counted.

getPercentUsed

public double getPercentUsed()
Answer, what percentage of the requested time has elapsed?

Returns:
0.0 if the timer has been started; 1.0 if the timer has been stopped (even if it didn't use all its requested time before being stopped); other wise, a value between 0.0 and 1.0 (paused time is not counted). A value of 1.0 means the timer is not running -- it's either expired or stopped.

getTimeRemaining

public double getTimeRemaining()
Answer the amount of time remaining before expiration.

Returns:
the time in seconds. After expiration or stopping, 0.0 is always returned. The paused status doesn't affect the answer.

getOriginallyRequestedTime

public double getOriginallyRequestedTime()
Answer, how much time was originally requested for this timer?

Returns:
the total time requested, in seconds. This value never changes.

addEggTimerListener

public void addEggTimerListener(EggTimerListener listener)
Add an EggTimerEvent listener


removeEggTimerListener

public void removeEggTimerListener(EggTimerListener listener)
Remove the givien EggTimerListener


moveBy

public boolean moveBy(int dx,
                      int dy)
Move the visible timer by the given amount (pixels). Moving off the screen is not allowed.

Returns:
true if the move was carried out, false otherwise

toString

public java.lang.String toString()
Returns a String describing the egg timer.

Overrides:
toString in class java.awt.Component

processEvent

public void processEvent(java.awt.AWTEvent event)
Override the JCompoment's processEvent method. We do this to intercept and dispatch any EggTimerRunoutEvents. All other events are sent to the superclass (JWindow) for normal processing. [Not for use by normal clients.]

Overrides:
processEvent in class java.awt.Window

main

public static void main(java.lang.String[] args)
Just a little test method. Doesn't test listener interface. The jiggling is only in the test method -- not built into the timer!