Class DragTutorial


  • public class DragTutorial
    extends java.lang.Object
    An example showing how to make figures draggable with interactors. Drag Tutorial 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$
    Author:
    John Reekie
    • Constructor Summary

      Constructors 
      Constructor Description
      DragTutorial()
      Create a JCanvas and put it into a window.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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