Class MouseTracker

  • All Implemented Interfaces:
    InputObject, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable, java.util.EventListener

    public class MouseTracker
    extends Drawable
    implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, InputObject
    A MouseTracker can be added to a CoordinateRect in a DisplayCanvas to respond to user mouse actions in the rectangular area occupied by the CoordinateRect. Two Variable objects, which can be retrieved by calling getXVar() and getYVar(), represent the location of the most recent mouse action in terms of the coordinates of the CoordinateRect. Note that these variables are Tieable objects, so they can be synchronized with other means of inputting the same information. The default names of the variables, if you don't change them, are "xMouse" and "yMouse".

    A MouseTracker is an InputObject. The values of the variables associated with the MouseTracker can change only when the checkInput() method is called (or when the setVal() method of the variable is called to set its value explicitely). If you want the value of the variables to track the mouse, you must add the MouseTracker (or the DisplayCanvas that contains it) to a Controller and set that Controller to listen for changes from the MouseTracker object by passing the Controller to the setOnUserAction() method of this class.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean clampX
      If thie is true, then the value of the variable associated with the x-ccordinate of the mouse is clamped to lie within the xmin and xmax of the coordinate rect.
      protected boolean clampY
      If thie is true, then the value of the variable associated with the y-ccordinate of the mouse is clamped to lie within the ymin and ymax of the coordinate rect.
      protected boolean listenForDrags
      If true, the MouseTracker responds to both clicks and drags.
      protected Controller onUserAction
      If this is non-null, then its compute() method is called when the user clicks the mouse and, if listenForDrags is also true, when the user drags and releases the mouse.
      protected boolean undefinedWhenNotDragging
      If true, the values of the associated variables are undefined except during the time that the user is clicking and dragging the mouse.
    • Constructor Summary

      Constructors 
      Constructor Description
      MouseTracker()
      Create a MouseTracker that responds to both clicks and drags.
      MouseTracker​(boolean listenForDrags, boolean undefinedWhenNotDragging)
      Creates a mouse tracker.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkInput()
      Set the values of the associated variables.
      void draw​(java.awt.Graphics g, boolean coordsChanged)
      A MouseTracker doesn't actually draw anything, but this method is required in a Drawable object.
      boolean getClampX()
      Get the "clampX" property of the MouseTracker.
      boolean getClampY()
      Get the "clampY" property of the MouseTracker.
      boolean getListenForDrags()
      Gets the "listenForDrags" property of the MouseTracker, which determines if the MouseTracker responds to both clicks and drags, or only to clicks.
      Controller getOnUserAction()
      Get the Controller that responds when a user mouse action is detected by this MouseTracker.
      boolean getUndefinedWhenNotDragging()
      Gets the "undefinedWhenNotDragging" property of the MouseTracker.
      Variable getXVar()
      Get the variable whose value represents the x-coordinate of the MouseTracker.
      Variable getYVar()
      Get the variable whose value represents the y-coordinate of the MouseTracker.
      void mouseClicked​(java.awt.event.MouseEvent evt)
      Empty method, required by MouseListener interface.
      void mouseDragged​(java.awt.event.MouseEvent evt)
      Responds when the user drags the mouse.
      void mouseEntered​(java.awt.event.MouseEvent evt)
      Empty method, required by MouseMotionListener interface.
      void mouseExited​(java.awt.event.MouseEvent evt)
      Empty method, required by MouseMotionListener interface.
      void mouseMoved​(java.awt.event.MouseEvent evt)
      Empty method, required by MouseMotionListener interface.
      void mousePressed​(java.awt.event.MouseEvent evt)
      Responds when the user clicks the mouse in the rectangular area occupied by the CoordinateRect that contains this MouseTracker.
      void mouseReleased​(java.awt.event.MouseEvent evt)
      Responds when the user releases the mouse.
      void notifyControllerOnChange​(Controller c)
      Method required by InputObject interface; in this class, it simply calls setOnUserAction(c).
      void setClampX​(boolean clamp)
      Set the "clampX" property of the MouseTracker.
      void setClampY​(boolean clamp)
      Set the "clampY" property of the MouseTracker.
      void setListenForDrags​(boolean listen)
      Sets the "listenForDrags" property of the MouseTracker.
      void setOnUserAction​(Controller onUserAction)
      Set a Controller to respond to user mouse actions tracked by this MouseTracker.
      protected void setOwnerData​(DisplayCanvas canvas, CoordinateRect coords)
      This is called automatically by CoordinateRect when the MouseTracker is added to the CoordinateRect.
      void setUndefinedWhenNotDragging​(boolean b)
      Sets the "undefinedWhenNotDragging" property of the MouseTracker.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • listenForDrags

        protected boolean listenForDrags
        If true, the MouseTracker responds to both clicks and drags. If false, it responds only to clicks.
      • undefinedWhenNotDragging

        protected boolean undefinedWhenNotDragging
        If true, the values of the associated variables are undefined except during the time that the user is clicking and dragging the mouse. This is ignored if listenForDrags is false.
      • onUserAction

        protected Controller onUserAction
        If this is non-null, then its compute() method is called when the user clicks the mouse and, if listenForDrags is also true, when the user drags and releases the mouse.
      • clampX

        protected boolean clampX
        If thie is true, then the value of the variable associated with the x-ccordinate of the mouse is clamped to lie within the xmin and xmax of the coordinate rect.
      • clampY

        protected boolean clampY
        If thie is true, then the value of the variable associated with the y-ccordinate of the mouse is clamped to lie within the ymin and ymax of the coordinate rect.
    • Constructor Detail

      • MouseTracker

        public MouseTracker()
        Create a MouseTracker that responds to both clicks and drags. The values of the associated variables remain defined even after the user stops dragging.
      • MouseTracker

        public MouseTracker​(boolean listenForDrags,
                            boolean undefinedWhenNotDragging)
        Creates a mouse tracker. The first parameter specifies whether the values of the variables change when the user drags the mouse, or only when the user clicks. The second parameter is only used if the first is true. It specifies whether the values of the variables become undefined after the user stops dragging the mouse.
    • Method Detail

      • getXVar

        public Variable getXVar()
        Get the variable whose value represents the x-coordinate of the MouseTracker. Note that this variable implements the Tieable interface, so can legally be type-cast to type Tieable. It can be tied to other objects that implement the Tieable and Value interfaces to synchronize their values.
      • getYVar

        public Variable getYVar()
        Get the variable whose value represents the y-coordinate of the MouseTracker. Note that this variable implements the Tieable interface, so can legally be type-cast to type Tieable. It can be tied to other objects that implement the Tieable and Value interfaces to synchronize their values.
      • setListenForDrags

        public void setListenForDrags​(boolean listen)
        Sets the "listenForDrags" property of the MouseTracker. If set to true, then the MouseTracker responds to both clicks and drags if false, it responds only to clicks.
      • getListenForDrags

        public boolean getListenForDrags()
        Gets the "listenForDrags" property of the MouseTracker, which determines if the MouseTracker responds to both clicks and drags, or only to clicks.
      • setUndefinedWhenNotDragging

        public void setUndefinedWhenNotDragging​(boolean b)
        Sets the "undefinedWhenNotDragging" property of the MouseTracker. This is ignored if the MouseTracker is not listening for drags. If set to true, the values of the variables associated with this variable become undefined when the user is not dragging.
      • getUndefinedWhenNotDragging

        public boolean getUndefinedWhenNotDragging()
        Gets the "undefinedWhenNotDragging" property of the MouseTracker.
      • setOnUserAction

        public void setOnUserAction​(Controller onUserAction)
        Set a Controller to respond to user mouse actions tracked by this MouseTracker. The MouseTracker should also be added to the Controller, so that the values of its variables will actually change when a user action occurs.
      • notifyControllerOnChange

        public void notifyControllerOnChange​(Controller c)
        Method required by InputObject interface; in this class, it simply calls setOnUserAction(c). This is meant to be called by JCMPanel.gatherInputs().
        Specified by:
        notifyControllerOnChange in interface InputObject
      • getOnUserAction

        public Controller getOnUserAction()
        Get the Controller that responds when a user mouse action is detected by this MouseTracker.
      • setClampX

        public void setClampX​(boolean clamp)
        Set the "clampX" property of the MouseTracker. If set to true, which is the default, the value of the variable associated with the horizontal position of the mouse is clamped to lie within the containing CoordinateRect.
      • getClampX

        public boolean getClampX()
        Get the "clampX" property of the MouseTracker.
      • setClampY

        public void setClampY​(boolean clamp)
        Set the "clampY" property of the MouseTracker. If set to true, which is the default, the value of the variable associated with the vertical position of the mouse is clamped to lie within the containing CoordinateRect.
      • getClampY

        public boolean getClampY()
        Get the "clampY" property of the MouseTracker.
      • checkInput

        public void checkInput()
        Set the values of the associated variables. This is part of the InputObject interface, and it is meant to be called by a Controller.
        Specified by:
        checkInput in interface InputObject
      • draw

        public void draw​(java.awt.Graphics g,
                         boolean coordsChanged)
        A MouseTracker doesn't actually draw anything, but this method is required in a Drawable object.
        Specified by:
        draw in class Drawable
        Parameters:
        g - The graphics context in which the Drawble is to be drawn. (The drawing can change the color in g, but should not permanently change font, painting mode, etc. Thus, every drawable is responsible for setting the color it wants to use.)
        coordsChanged - Indicates whether the CoordinateRect has changed.
      • setOwnerData

        protected void setOwnerData​(DisplayCanvas canvas,
                                    CoordinateRect coords)
        This is called automatically by CoordinateRect when the MouseTracker is added to the CoordinateRect. It is not meant to be used directly.
        Overrides:
        setOwnerData in class Drawable
      • mousePressed

        public void mousePressed​(java.awt.event.MouseEvent evt)
        Responds when the user clicks the mouse in the rectangular area occupied by the CoordinateRect that contains this MouseTracker. Since the MouseTracker listens for clicks on the whole DisplayCanvas and the CoordinateRect might only occupy part of that, it is necessary to check whether the user click was in that rect. This is not meant to be called directly.
        Specified by:
        mousePressed in interface java.awt.event.MouseListener
      • mouseReleased

        public void mouseReleased​(java.awt.event.MouseEvent evt)
        Responds when the user releases the mouse. This is not meant to be called directly.
        Specified by:
        mouseReleased in interface java.awt.event.MouseListener
      • mouseDragged

        public void mouseDragged​(java.awt.event.MouseEvent evt)
        Responds when the user drags the mouse. This is not meant to be called directly.
        Specified by:
        mouseDragged in interface java.awt.event.MouseMotionListener
      • mouseClicked

        public void mouseClicked​(java.awt.event.MouseEvent evt)
        Empty method, required by MouseListener interface.
        Specified by:
        mouseClicked in interface java.awt.event.MouseListener
      • mouseEntered

        public void mouseEntered​(java.awt.event.MouseEvent evt)
        Empty method, required by MouseMotionListener interface.
        Specified by:
        mouseEntered in interface java.awt.event.MouseListener
      • mouseExited

        public void mouseExited​(java.awt.event.MouseEvent evt)
        Empty method, required by MouseMotionListener interface.
        Specified by:
        mouseExited in interface java.awt.event.MouseListener
      • mouseMoved

        public void mouseMoved​(java.awt.event.MouseEvent evt)
        Empty method, required by MouseMotionListener interface.
        Specified by:
        mouseMoved in interface java.awt.event.MouseMotionListener