public class DragTutorial
extends java.lang.Object
To make a figure draggable, we create an instance of DragInteractor and attach it to the figure, like this:
Interactor dragger = new DragInteractor(); dragger.setMouseFilter(MouseFilter.defaultFilter); BasicFigure blue = new BasicRectangle(10.0,10.0,50.0,50.0,Color.blue); layer.add(blue); blue.setInteractor(dragger);The mouse filter is used to tell the interactor whether or not to respond to events. The default mouse filter used here is button 1 with no modifiers.
Each interactor can also have a set of constraints added to it. In this example, we have created an instance of BoundedDragInteractor, which adds a constraint to itself in its constructor. BoundedDragInteractor always keeps figures that it is dragging within a rectangular region -- in our example, the region is shown by the grey line.
One point to note about interactors. In general, many figures will share a single interactor. For example, all nodes in a graph editor might reference the "node interactor" object. This strategy allows the behaviour of a whole set of figures to be changed together (by changing the behaviour of the interactor).
Constructor and Description |
---|
DragTutorial()
Create a JCanvas and put it into a window.
|
Modifier and Type | Method and Description |
---|---|
void |
createBoundedDraggableFigure()
Create another simple figures and make it draggable within
a region of the canvas.
|
void |
createDraggableFigures()
Create a couple of simple figures and make them draggable.
|
static void |
main(java.lang.String[] argv)
Main function
|
public void createDraggableFigures()
public void createBoundedDraggableFigure()
public static void main(java.lang.String[] argv)