Class ARubberband

java.lang.Object
edu.cnu.mdi.graphics.rubberband.ARubberband
All Implemented Interfaces:
MouseListener, MouseMotionListener, EventListener
Direct Known Subclasses:
AClickRubberband, ADragRubberband

public abstract class ARubberband extends Object implements MouseListener, MouseMotionListener
Base class for all rubberband gestures. Owns: - lifecycle (start/end/cancel) - image buffering + overlay drawing - shared points (start/current) - shared poly storage (poly/tempPoly) for click-collected gestures Subclasses own: - interaction semantics (drag vs click) - shape drawing - bounds + vertices + validity rules (if not area-based)
  • Field Details

    • fillColor

      protected Color fillColor
    • highlightColor1

      protected Color highlightColor1
    • highlightColor2

      protected Color highlightColor2
    • policy

      protected final ARubberband.Policy policy
      The policy this instance was constructed under. Purely descriptive metadata for the instance's identity/factory dispatch (e.g. by RubberbandFactory); actual behavior is determined by the concrete subclass's method overrides, not by branching on this field.
    • startPt

      protected final Point startPt
    • currentPt

      protected final Point currentPt
    • component

      protected final Component component
    • rubberbanded

      protected final IRubberbanded rubberbanded
    • started

      protected boolean started
    • active

      protected boolean active
    • backgroundImage

      protected BufferedImage backgroundImage
      Buffered image of the component at the start of the gesture, used for clean rubberband redraws.
    • image

      protected BufferedImage image
    • poly

      protected Polygon poly
      Final poly result after completion (poly policies).
    • tempPoly

      protected Polygon tempPoly
      Working poly during collection/preview (click policies).
  • Constructor Details

    • ARubberband

      protected ARubberband(Component component, IRubberbanded rubberbanded, ARubberband.Policy policy)
      Constructor does not activate; caller must call setActive(true) to enable listening.
      Parameters:
      component - target component for mouse events and drawing
      rubberbanded - callback for completion notification
      policy - rubberband policy; stored as descriptive metadata (see policy), not consulted internally
  • Method Details

    • begin

      public final void begin(Point anchorPt)
      Begin rubberbanding with the given anchor point. For drag-based policies, this is typically called from mousePressed(); for toolbar-initiated gestures, this is called by the toolbar to start the gesture with a specific anchor point.
      Parameters:
      anchorPt - the initial point of the gesture; must not be null
    • isActive

      public final boolean isActive()
      Active means listening and rubberbanding in response to mouse events.
      Returns:
      true if this rubberband is active and responding to mouse events, false if inactive or completed (waiting for toolbar to read results).
    • setActive

      public final void setActive(boolean b)
      Activate or deactivate this rubberband. Deactivated rubberbands ignore mouse events and do not draw.
      Parameters:
      b - true to activate, false to deactivate. Deactivation is not the same as completion; it simply ignores mouse events and stops drawing until reactivated or completed.
    • isClickBased

      public boolean isClickBased()
      True if this rubberband collects points on click rather than drag. Used by toolbars to determine when to call isGestureValid() and getRubberbandVertices().
      Returns:
      true if this rubberband collects points on click rather than drag, false if it is drag-based.
    • isGestureValid

      public boolean isGestureValid(int minSizePx)
      Validity rules depend on the policy; default is area-based (bounds must exceed min size). Click-based policies should override with their own rules.
      Parameters:
      minSizePx - minimum size in pixels for validity; interpretation depends on policy (e.g. max dimension for polygons).
      Returns:
      true if the current gesture state is valid for completion, false if it should be rejected (e.g. too small).
    • getStart

      public final Point getStart()
      Get the anchor point where the gesture started. For click-based policies, this may be the first click or a synthetic point; for drag-based policies, this is the initial mouse press location.
      Returns:
      the start point of the gesture, which may be used as a reference for drawing and bounds calculations.
    • getCurrent

      public final Point getCurrent()
      Get the current point of the gesture, which may be updated during dragging or clicking. For click-based policies, this may be the most recent click location or a synthetic point; for drag-based policies, this is the current mouse location during dragging.
      Returns:
      the current point of the gesture, which may be used as a reference for drawing and bounds calculations.
    • setFillColor

      public final void setFillColor(Color color)
      Set the fill color for this rubberband. Used for the main shape fill; subclasses may also use it for borders or highlights.
      Parameters:
      color - the fill color to use when drawing this rubberband; may be null for no fill.
    • setHighlightColor1

      public final void setHighlightColor1(Color c)
      Primary highlight color, used for borders or 1-color gradients.
      Parameters:
      c - the primary highlight color to use when drawing this rubberband; may be null if not used.
    • setHighlightColor2

      public final void setHighlightColor2(Color c)
      Secondary highlight color, used for 2-color gradients or borders.
      Parameters:
      c - the secondary highlight color to use when drawing this rubberband; may be null if not used.
    • cancel

      public void cancel()
      Cancel the rubberbanding operation. Discards results and releases resources immediately. The toolbar will not call doneRubberbanding() after this, so the rubberband should not rely on that for cleanup.
    • endRubberbanding

      public void endRubberbanding(Point p)
      Normal completion. Preserves results long enough for the toolbar to read them in IRubberbanded.doneRubberbanding().
      Parameters:
      p - the final point of the gesture, which may be used for final drawing and bounds calculations; may be null if not applicable.
    • setCurrent

      protected final void setCurrent(Point newCurrentPoint)
      Update the current point and redraw the rubberband. For drag-based policies, this is called during dragging; for click-based policies, this may be called after each click or when the toolbar requests a preview update.
      Parameters:
      newCurrentPoint -
    • addPoint

      protected final void addPoint(Polygon p, int x, int y)
      Add a point to the given polygon if it's not a duplicate of the last point. Used by click-based policies to collect vertices.
      Parameters:
      p - the polygon to add the point to; must not be null
      x - the x-coordinate of the point to add
      y - the y-coordinate of the point to add
    • modifyCurrentPoint

      protected void modifyCurrentPoint(Point cp)
      Hook to modify the current point before using it. Default: clamp to component bounds (old Rubberband behavior).
    • startRubberbanding

      protected void startRubberbanding(Point anchorPt)
      Initialize rubberbanding state and capture the component image for drawing. Called at the start of the gesture, either from mousePressed() for drag-based policies or from begin() for toolbar-initiated gestures.
      Parameters:
      anchorPt - the initial point of the gesture; must not be null
    • draw

      protected abstract void draw(Graphics2D g)
      Draw the rubberband shape based on the current state. Called during dragging or when the toolbar requests a preview update. Subclasses must implement this to define the visual appearance of the rubberband.
      Parameters:
      g -
    • getRubberbandBounds

      public abstract Rectangle getRubberbandBounds()
      Get the bounding rectangle of the rubberband shape based on the current state. Used for area-based validity checks and as a default for vertex calculations. Subclasses should override this if their shape is not well-represented by a bounding rectangle (e.g. lines, polygons).
      Returns:
      the bounding rectangle of the rubberband shape, or null if not applicable.
    • computeVertices

      protected Point[] computeVertices()
      Get the vertices of the rubberband shape based on the current state. Used for click-based policies and by toolbars to read results. Default implementation returns the corners of the bounding rectangle, which is suitable for rectangles and ovals but not for lines or polygons. Subclasses should override this if their shape has specific vertex requirements.
      Returns:
      an array of points representing the vertices of the rubberband shape, or null if not applicable.
    • getRubberbandVertices

      public final Point[] getRubberbandVertices()
      Get the vertices of the rubberband shape for click-based policies. For drag-based policies, this returns the corners of the bounding rectangle by default.
      Returns:
      an array of points representing the vertices of the rubberband shape, or null if not applicable. For click-based policies, this should return the collected vertices; for drag-based policies, this returns the corners of the bounding rectangle by default.
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Specified by:
      mouseClicked in interface MouseListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

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

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

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

      public void mouseDragged(MouseEvent e)
      Specified by:
      mouseDragged in interface MouseMotionListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Specified by:
      mouseMoved in interface MouseMotionListener