Package selector

Class SelectionComponent

All Implemented Interfaces:
MouseListener, MouseMotionListener, ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener

public class SelectionComponent extends JComponent implements MouseListener, MouseMotionListener, PropertyChangeListener
A transparent (overlay) component enabling interactive selection (aka "tracing") of an underlying image. Layout must ensure that our upper-left corner coincides with that of the underlying image view.
See Also:
  • Constructor Details

    • SelectionComponent

      public SelectionComponent(SelectionModel model)
      Construct a new SelectionComponent that will participate in viewing and controlling the selection modeled by `model`. View will update upon receiving property change events from `model`.
  • Method Details

    • setModel

      public void setModel(SelectionModel newModel)
      Have this component view and control `newModel` instead of whichever model it was using previously. This component will no longer react to events from the old model. If a point from the previous selection was being moved, that interaction is discarded.
    • getModel

      public SelectionModel getModel()
      Return the selection model currently being viewed and controlled by this component.
    • paintComponent

      public void paintComponent(Graphics g)
      Visualize our model's state, as well as our interaction state, by drawing our view using `g`.
      Overrides:
      paintComponent in class JComponent
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      When mouse button 1 is clicked and a selection has either not yet been started or is still in progress, add the location of the point to the selection. Note: `mousePressed()` and `mouseReleased()` handle presses of button 1 when the selection is finished.

      When mouse button 2 is clicked and a selection is in progress, finish the selection.

      When mouse button 3 is clicked and a selection is either in progress or finished, undo the last point added to the selection.

      Specified by:
      mouseClicked in interface MouseListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      When a selection is in progress, update our last-observed mouse location to the location of this event and repaint ourselves to draw a "live wire" to the mouse pointer.
      Specified by:
      mouseMoved in interface MouseMotionListener
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      When a selection is in progress, or when we are interacting with a control point, update our last-observed mouse location to the location of this event and repaint ourselves to draw a "live wire" to the mouse pointer. (Note that mouseMoved events are not sent while dragging, which is why this overlaps with the duties of that handler.)
      Specified by:
      mouseDragged in interface MouseMotionListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      When mouse button 1 is pressed while our model's selection is complete, search for a control point close to the mouse pointer and, if found, start interacting with that point. A point is only eligible for interaction if the mouse was pressed within the circle representing it in the view.
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      When mouse button 1 is released while we are interacting with a control point, move the selected point to the current mouse location.
      Specified by:
      mouseReleased in interface MouseListener
    • propertyChange

      public void propertyChange(PropertyChangeEvent e)
      Repaint to update our view in response to any property changes from our model. Additionally, if the "image" property changed, update our preferred size to match the new image size.
      Specified by:
      propertyChange in interface PropertyChangeListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Specified by:
      mouseExited in interface MouseListener