com.lavantech.gui.comp
Class ClockPanel

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended bycom.lavantech.gui.comp.ClockPanel
All Implemented Interfaces:
java.util.EventListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable, java.io.Serializable

public class ClockPanel
extends javax.swing.JComponent
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable

This Component displays a Analog Clock for a given GregorianCalendar. The ClockPanel can be used to display a live Clock. Here is an example.

  import com.lavantech.gui.comp.*;
  import java.util.*;
  import java.awt.*;
  import javax.swing.*;

  class LiveClock extends JPanel implements Runnable
  {
      Thread updateThread = null;
      public boolean liveMode = true;
      ClockPanel clockPanel = null;

      public LiveClock()
      {
          super(new BorderLayout());
          clockPanel = new ClockPanel(new GregorianCalendar());
          add(clockPanel, BorderLayout.CENTER);
          updateThread = new Thread(this);
          updateThread.start();
      }
      
      public void run()
      {
          while(liveMode)
          {
              try
              {
                  Thread.sleep(1000);
              }
              catch(Exception exp)
              {
              }
              clockPanel.setCalendar(new GregorianCalendar());
              SwingUtilities.invokeLater(clockPanel);
          }
      }
      
      public static void main(String args[])
      {
           JFrame frame = new JFrame();
           frame.getContentPane().add(new LiveClock());
           frame.pack();
           frame.show();
      }
  }
  

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
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
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
ClockPanel(java.util.GregorianCalendar cal)
          Constructs a TimePanel with the given GregorianCalendar
ClockPanel(java.util.GregorianCalendar cal, boolean editable)
          Constructs a TimePanel with the given GregorianCalendar
 
Method Summary
 void addActionListener(java.awt.event.ActionListener ls)
          Add an action listener that will be notified when the time is changed.
 java.util.GregorianCalendar getCalendar()
          Returns the selected time.
 java.awt.Color getFaceColor()
          Returns the clock's face color in the default face graphics.
 java.awt.Image getFaceImage()
          Gets the image used for the clock face.
 int getHourFormat()
          Gets the hour format for the clock.
 java.awt.Color getHourNeedleColor()
          Returns the clock's hour needle's color.
 double getHourNeedleHeightRatio()
          Returns the hour needle height to the clock face radius ratio.
 java.awt.Shape getHourNeedleShape()
          Returns the shape of the hour needle.
 double getHourNeedleWidthRatio()
          Returns the hour needle width to the clock face radius ratio.
 java.util.GregorianCalendar getMaxSelectableTime()
          Returns the maximum time that can be selected.
 java.awt.Color getMinNeedleColor()
          Gets the clock's minute needle's color
 double getMinNeedleHeightRatio()
          Returns the minute needle height to the clock face radius ratio.
 java.awt.Shape getMinNeedleShape()
          Returns the shape of the minute needle.
 double getMinNeedleWidthRatio()
          Returns the minute needle width to the clock face radius ratio.
 java.util.GregorianCalendar getMinSelectableTime()
          Returns the minimum time that can be selected.
 java.awt.Color getSecNeedleColor()
          Gets the clock's second needle's color
 double getSecNeedleHeightRatio()
          Returns the second needle height to the clock face radius ratio.
 java.awt.Shape getSecNeedleShape()
          Returns the shape of the seconds needle.
 double getSecNeedleWidthRatio()
          Returns the second needle width to the clock face radius ratio.
 boolean isEditable()
          Returns if this ClockPanel is editable
 boolean isMinDisplayed()
          Returns whether the minute needle is displayed or not.
 boolean isSecDisplayed()
          Returns whether the second needle is displayed or not.
 void mouseClicked(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mouseDragged(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mouseEntered(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mouseExited(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mouseMoved(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mousePressed(java.awt.event.MouseEvent evt)
          Implementation side effect.
 void mouseReleased(java.awt.event.MouseEvent evt)
          Implementation side effect.
protected  void paintComponent(java.awt.Graphics g)
           
 void removeActionListener(java.awt.event.ActionListener ls)
          Remove an action listener.
 void run()
          The run method updates the ClockPanel just like updateTime().
 void setCalendar(java.util.GregorianCalendar cal)
          Sets the selected time.
 void setEditable(boolean editable)
          Sets if the clock is changable
 void setEnabled(boolean enable)
          Sets whether the ClockPanel is enabled or not.
 void setFaceColor(java.awt.Color color)
          Sets the clock's face color in the default face graphics.
 void setFaceImage(java.awt.Image face)
          Sets the image to use for the clock face.
 void setHourFormat(int format)
          Sets the hour format for the clock.
 void setHourNeedleColor(java.awt.Color color)
          Sets the clock's hour needle's color.
 void setHourNeedleHeightRatio(double ratio)
          Sets the relative height of hour needle to the clock face radius.
 void setHourNeedleShape(java.awt.Shape newShape)
          Sets the hour needle shape.
 void setHourNeedleWidthRatio(double ratio)
          Sets the relative width of hour needle to the clock face radius.
 void setMaxSelectableTime(java.util.GregorianCalendar maxTime)
          Set the maximum time that can be selected.
 void setMinDisplayed(boolean val)
          Sets whether the minute needle is displayed or not.
 void setMinNeedleColor(java.awt.Color color)
          Sets the clock's minute needle's color.
 void setMinNeedleHeightRatio(double ratio)
          Sets the relative height of minute needle to the clock face radius.
 void setMinNeedleShape(java.awt.Shape newShape)
          Sets the minute needle shape.
 void setMinNeedleWidthRatio(double ratio)
          Sets the relative width of minute needle to the clock face radius.
 void setMinSelectableTime(java.util.GregorianCalendar minTime)
          Set the minimum time that can be selected.
 void setSecDisplayed(boolean val)
          Sets whether the second needle is displayed or not.
 void setSecNeedleColor(java.awt.Color color)
          Sets the clock's second needle's color.
 void setSecNeedleHeightRatio(double ratio)
          Sets the relative height of second needle to the clock face radius.
 void setSecNeedleShape(java.awt.Shape newShape)
          Sets the second needle shape.
 void setSecNeedleWidthRatio(double ratio)
          Sets the relative width of second needle to the clock face radius.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, 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, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClockPanel

public ClockPanel(java.util.GregorianCalendar cal)
Constructs a TimePanel with the given GregorianCalendar

Parameters:
cal - The calendar time to which the clock will be initially set to.

ClockPanel

public ClockPanel(java.util.GregorianCalendar cal,
                  boolean editable)
Constructs a TimePanel with the given GregorianCalendar

Parameters:
cal - The calendar time to which the clock will be initially set to.
editable - If true, User can change the clock by dragging the needles.
Method Detail

getMinSelectableTime

public java.util.GregorianCalendar getMinSelectableTime()
Returns the minimum time that can be selected. If there is no minimum time limit, null is returned.

Returns:
GregorianCalendar Minimum time that can be selected or null if no minimum time limit.

setMinSelectableTime

public void setMinSelectableTime(java.util.GregorianCalendar minTime)
Set the minimum time that can be selected. If the current selected time is less than the minimum time, the current selected time is set to the minimum time.

Parameters:
minTime - Minimum time that can be selected in DateTimePicker. null can be passed to remove minimum limit.
Throws:
java.lang.IllegalArgumentException - If minTime is greater than maxSelectableTime

getMaxSelectableTime

public java.util.GregorianCalendar getMaxSelectableTime()
Returns the maximum time that can be selected. If there is no maximum time limit, null is returned.

Returns:
GregorianCalendar Maximum time that can be selected or null if no maximum time limit.

setMaxSelectableTime

public void setMaxSelectableTime(java.util.GregorianCalendar maxTime)
Set the maximum time that can be selected. If the current selected time is greater than the maximum time, the current selected time is set to the maximum time.

Parameters:
maxTime - Maximum time that can be selected in DateTimePicker. null can be passed to remove maximum limit.
Throws:
java.lang.IllegalArgumentException - If maxTime is less than minSelectableTime

setEnabled

public void setEnabled(boolean enable)
Sets whether the ClockPanel is enabled or not.


setEditable

public void setEditable(boolean editable)
Sets if the clock is changable

Parameters:
editable - If true, User can change the clock by dragging the needles.

isEditable

public boolean isEditable()
Returns if this ClockPanel is editable


paintComponent

protected void paintComponent(java.awt.Graphics g)

getCalendar

public java.util.GregorianCalendar getCalendar()
Returns the selected time.


setCalendar

public void setCalendar(java.util.GregorianCalendar cal)
Sets the selected time.


run

public void run()
The run method updates the ClockPanel just like updateTime(). This method is used to implement the Runnable interface, the Runnable interface can be used to update the clock from another thread using the SwingUtilies.invokeLater() method.

Specified by:
run in interface java.lang.Runnable

setHourFormat

public void setHourFormat(int format)
Sets the hour format for the clock. The two possible values are LocaleSpecificResource.HOUR_FORMAT_12 and LocaleSpecificResource.HOUR_FORMAT_24. The 12 hour format is the default value.


getHourFormat

public int getHourFormat()
Gets the hour format for the clock. The hour format returned is either LocaleSpecificResource.HOUR_FORMAT_12 or LocaleSpecificResource.HOUR_FORMAT_24.


getFaceColor

public java.awt.Color getFaceColor()
Returns the clock's face color in the default face graphics. The default face graphics is used when no faceImage is set.


setFaceColor

public void setFaceColor(java.awt.Color color)
Sets the clock's face color in the default face graphics. The default face graphics is used when no faceImage is set.


getHourNeedleColor

public java.awt.Color getHourNeedleColor()
Returns the clock's hour needle's color.


setHourNeedleColor

public void setHourNeedleColor(java.awt.Color color)
Sets the clock's hour needle's color.


getMinNeedleColor

public java.awt.Color getMinNeedleColor()
Gets the clock's minute needle's color


setMinNeedleColor

public void setMinNeedleColor(java.awt.Color color)
Sets the clock's minute needle's color.


getSecNeedleColor

public java.awt.Color getSecNeedleColor()
Gets the clock's second needle's color


setSecNeedleColor

public void setSecNeedleColor(java.awt.Color color)
Sets the clock's second needle's color.


getFaceImage

public java.awt.Image getFaceImage()
Gets the image used for the clock face. If no image was set, null is returned.


setFaceImage

public void setFaceImage(java.awt.Image face)
Sets the image to use for the clock face. If set to null, the default face graphics is used.


getHourNeedleShape

public java.awt.Shape getHourNeedleShape()
Returns the shape of the hour needle.


setHourNeedleShape

public void setHourNeedleShape(java.awt.Shape newShape)
Sets the hour needle shape. The shape should be pointing north (12oClock position) and the pivot center of the needle should be coordinate 0,0 (x,y). The shape can be of any size. The shape is scaled to a height and width relative to clock radius. The hourNeedleHeightRatio and hourNeedleWidthRatio properties determine the relative height and width. If null is passed, the shape is set to the default shape. The default shape is a polygon with the vertices (5,5) , (0,8), (-5,5),(-5,-85), (0,-100), (5,-85), (5,5).

See Also:
setHourNeedleHeightRatio(double), setHourNeedleWidthRatio(double)

getMinNeedleShape

public java.awt.Shape getMinNeedleShape()
Returns the shape of the minute needle.


setMinNeedleShape

public void setMinNeedleShape(java.awt.Shape newShape)
Sets the minute needle shape. The shape should be pointing north (12oClock position) and the pivot center of the needle should be coordinate 0,0 (x,y). The shape can be of any size. The shape is scaled to a height and width relative to clock radius. The minNeedleHeightRatio and minNeedleWidthRatio properties determine the relative height and width. If null is passed, the shape is set to the default shape. The default shape is a polygon with the vertices (5,5) , (0,8), (-5,5),(-5,-85), (0,-100), (5,-85), (5,5).

See Also:
setMinNeedleHeightRatio(double), setMinNeedleWidthRatio(double)

getSecNeedleShape

public java.awt.Shape getSecNeedleShape()
Returns the shape of the seconds needle.


setSecNeedleShape

public void setSecNeedleShape(java.awt.Shape newShape)
Sets the second needle shape. The shape should be pointing north (12oClock position) and the pivot center of the needle should be coordinate 0,0 (x,y). The shape can be of any size. The shape is scaled to a height and width relative to clock radius. The secNeedleHeightRatio and secNeedleWidthRatio properties determine the relative height and width. If null is passed, the shape is set to the default shape. The default shape is a polygon with the vertices (5,5) , (0,8), (-5,5),(-5,-85), (0,-100), (5,-85), (5,5).

See Also:
setSecNeedleHeightRatio(double), setSecNeedleWidthRatio(double)

getHourNeedleHeightRatio

public double getHourNeedleHeightRatio()
Returns the hour needle height to the clock face radius ratio.


setHourNeedleHeightRatio

public void setHourNeedleHeightRatio(double ratio)
Sets the relative height of hour needle to the clock face radius. The value should be between 0 - 1.


getHourNeedleWidthRatio

public double getHourNeedleWidthRatio()
Returns the hour needle width to the clock face radius ratio.


setHourNeedleWidthRatio

public void setHourNeedleWidthRatio(double ratio)
Sets the relative width of hour needle to the clock face radius. The value should be between 0 - 1.


getMinNeedleHeightRatio

public double getMinNeedleHeightRatio()
Returns the minute needle height to the clock face radius ratio.


setMinNeedleHeightRatio

public void setMinNeedleHeightRatio(double ratio)
Sets the relative height of minute needle to the clock face radius. The value should be between 0 - 1.


getMinNeedleWidthRatio

public double getMinNeedleWidthRatio()
Returns the minute needle width to the clock face radius ratio.


setMinNeedleWidthRatio

public void setMinNeedleWidthRatio(double ratio)
Sets the relative width of minute needle to the clock face radius. The value should be between 0 - 1.


getSecNeedleHeightRatio

public double getSecNeedleHeightRatio()
Returns the second needle height to the clock face radius ratio.


setSecNeedleHeightRatio

public void setSecNeedleHeightRatio(double ratio)
Sets the relative height of second needle to the clock face radius. The value should be between 0 - 1.


getSecNeedleWidthRatio

public double getSecNeedleWidthRatio()
Returns the second needle width to the clock face radius ratio.


setSecNeedleWidthRatio

public void setSecNeedleWidthRatio(double ratio)
Sets the relative width of second needle to the clock face radius. The value should be between 0 - 1.


isMinDisplayed

public boolean isMinDisplayed()
Returns whether the minute needle is displayed or not.


setMinDisplayed

public void setMinDisplayed(boolean val)
Sets whether the minute needle is displayed or not.


isSecDisplayed

public boolean isSecDisplayed()
Returns whether the second needle is displayed or not.


setSecDisplayed

public void setSecDisplayed(boolean val)
Sets whether the second needle is displayed or not.


mouseClicked

public void mouseClicked(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent evt)
Implementation side effect.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

addActionListener

public void addActionListener(java.awt.event.ActionListener ls)
Add an action listener that will be notified when the time is changed.


removeActionListener

public void removeActionListener(java.awt.event.ActionListener ls)
Remove an action listener.