Package edu.cnu.mdi.graphics.rubberband
Class ARubberband
java.lang.Object
edu.cnu.mdi.graphics.rubberband.ARubberband
- All Implemented Interfaces:
MouseListener,MouseMotionListener,EventListener
- Direct Known Subclasses:
AClickRubberband,ADragRubberband
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRubberband policy, defining behavior and drawing rules. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected BufferedImageBuffered image of the component at the start of the gesture, used for clean rubberband redraws.protected final Componentprotected final Pointprotected Colorprotected Colorprotected Colorprotected BufferedImageprotected final ARubberband.PolicyThe policy this instance was constructed under.protected PolygonFinal poly result after completion (poly policies).protected final IRubberbandedprotected booleanprotected final Pointprotected PolygonWorking poly during collection/preview (click policies). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedARubberband(Component component, IRubberbanded rubberbanded, ARubberband.Policy policy) Constructor does not activate; caller must call setActive(true) to enable listening. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidAdd a point to the given polygon if it's not a duplicate of the last point.final voidBegin rubberbanding with the given anchor point.voidcancel()Cancel the rubberbanding operation.protected Point[]Get the vertices of the rubberband shape based on the current state.protected abstract voiddraw(Graphics2D g) Draw the rubberband shape based on the current state.voidNormal completion.final PointGet the current point of the gesture, which may be updated during dragging or clicking.abstract RectangleGet the bounding rectangle of the rubberband shape based on the current state.final Point[]Get the vertices of the rubberband shape for click-based policies.final PointgetStart()Get the anchor point where the gesture started.final booleanisActive()Active means listening and rubberbanding in response to mouse events.booleanTrue if this rubberband collects points on click rather than drag.booleanisGestureValid(int minSizePx) Validity rules depend on the policy; default is area-based (bounds must exceed min size).protected voidHook to modify the current point before using it.voidvoidvoidvoidvoidvoidvoidfinal voidsetActive(boolean b) Activate or deactivate this rubberband.protected final voidsetCurrent(Point newCurrentPoint) Update the current point and redraw the rubberband.final voidsetFillColor(Color color) Set the fill color for this rubberband.final voidPrimary highlight color, used for borders or 1-color gradients.final voidSecondary highlight color, used for 2-color gradients or borders.protected voidstartRubberbanding(Point anchorPt) Initialize rubberbanding state and capture the component image for drawing.
-
Field Details
-
fillColor
-
highlightColor1
-
highlightColor2
-
policy
The policy this instance was constructed under. Purely descriptive metadata for the instance's identity/factory dispatch (e.g. byRubberbandFactory); actual behavior is determined by the concrete subclass's method overrides, not by branching on this field. -
startPt
-
currentPt
-
component
-
rubberbanded
-
started
protected boolean started -
active
protected boolean active -
backgroundImage
Buffered image of the component at the start of the gesture, used for clean rubberband redraws. -
image
-
poly
Final poly result after completion (poly policies). -
tempPoly
Working poly during collection/preview (click policies).
-
-
Constructor Details
-
ARubberband
Constructor does not activate; caller must call setActive(true) to enable listening.- Parameters:
component- target component for mouse events and drawingrubberbanded- callback for completion notificationpolicy- rubberband policy; stored as descriptive metadata (seepolicy), not consulted internally
-
-
Method Details
-
begin
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 benull
-
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
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
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
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
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
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
Normal completion. Preserves results long enough for the toolbar to read them inIRubberbanded.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
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
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 nullx- the x-coordinate of the point to addy- the y-coordinate of the point to add
-
modifyCurrentPoint
Hook to modify the current point before using it. Default: clamp to component bounds (old Rubberband behavior). -
startRubberbanding
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 benull
-
draw
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
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
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
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
- Specified by:
mouseClickedin interfaceMouseListener
-
mousePressed
- Specified by:
mousePressedin interfaceMouseListener
-
mouseReleased
- Specified by:
mouseReleasedin interfaceMouseListener
-
mouseEntered
- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
- Specified by:
mouseExitedin interfaceMouseListener
-
mouseDragged
- Specified by:
mouseDraggedin interfaceMouseMotionListener
-
mouseMoved
- Specified by:
mouseMovedin interfaceMouseMotionListener
-