diva.canvas.tutorial
Class DragTutorial

java.lang.Object
  extended by diva.canvas.tutorial.DragTutorial

public class DragTutorial
extends java.lang.Object

An example showing how to make figures draggable with interactors. Each figure on the canvas can have its interactor set or read with the methods setInteractor() and getInteractor(). The interactor answers the question "What does this figure do when I mouse on it?" In this example, what the figure does is follow the mouse.

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).

Version:
$Id: DragTutorial.java 38798 2005-07-08 20:00:01Z cxh $
Author:
John Reekie

Field Summary
private  JCanvas canvas
           
private  GraphicsPane graphicsPane
           
 
Constructor Summary
DragTutorial()
          Create a JCanvas and put it into a window.
 
Method Summary
 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

canvas

private JCanvas canvas

graphicsPane

private GraphicsPane graphicsPane
Constructor Detail

DragTutorial

public DragTutorial()
Create a JCanvas and put it into a window.

Method Detail

createDraggableFigures

public void createDraggableFigures()
Create a couple of simple figures and make them draggable. Both figures are given the same interactor, which means that they behave the same when you mouse on them. The interactor used to move them is an instance of DragInteractor.


createBoundedDraggableFigure

public void createBoundedDraggableFigure()
Create another simple figures and make it draggable within a region of the canvas. This example uses an instance of BoundedDragInteractor to move the object.


main

public static void main(java.lang.String[] argv)
Main function