Use the DragEventTool

From JReality Wiki
Jump to: navigation, search

JavaDoc: DragEventTool

The DragEventTool is a tool which is designed to be customized.

  • It frees the user from directly interacting with the pick system
  • It notifies its listeners when picking events generate events in the scene graph where the tool is positioned.
  • There are separate listeners for point, line, and polygon drag events.

Simple example

Source file: DragEventTool01

Run as Java webstart

This example takes advantage of the difference between the picked point and the closest actual vertex of the geometric data. The listener replaces the actual vertex with the picked point. This has the effect of dragging the actual vertex in the plane parallel to the viewing plane.

  public static void main(String[] args) {
    SceneGraphComponent cmp = new SceneGraphComponent();
    Appearance ap = new Appearance();
    DragEventTool t = new DragEventTool();
    t.addPointDragListener(new PointDragListener() {
      public void pointDragStart(PointDragEvent e) {
        System.out.println("drag start of vertex no "+e.getIndex());        
      public void pointDragged(PointDragEvent e) {
        PointSet pointSet = e.getPointSet();
        double[][] points=new double[pointSet.getNumPoints()][];
      public void pointDragEnd(PointDragEvent e) {

Advanced example

Source file: DragEventTool02

Run as Java webstart

This example registers as listeners for point, line and face drag events, and moves the corresponding element rigidly. We only list the new methods required to handle lines and polygons.

Play with it to see how it works!

   t.addLineDragListener(new LineDragListener() {
      private IndexedLineSet lineSet;
      private double[][] points;
      public void lineDragStart(LineDragEvent e) {
        System.out.println("start dragging line "+e.getIndex());
        lineSet = e.getIndexedLineSet();
        points=new double[lineSet.getNumPoints()][];
      public void lineDragged(LineDragEvent e) {
        double[][] newPoints=(double[][])points.clone();
        Matrix trafo=new Matrix();
        int[] lineIndices=e.getLineIndices();
        for(int i=0;i<lineIndices.length;i++){
      public void lineDragEnd(LineDragEvent e) {
    t.addFaceDragListener(new FaceDragListener() {
      private IndexedFaceSet faceSet;
      private double[][] points;
      public void faceDragStart(FaceDragEvent e) {
        faceSet = e.getIndexedFaceSet();
        points=new double[faceSet.getNumPoints()][];
        points = faceSet.getVertexAttributes(Attribute.COORDINATES).toDoubleArrayArray(null);
      public void faceDragged(FaceDragEvent e) {
        double[][] newPoints=(double[][])points.clone();
        Matrix trafo=new Matrix();
        int[] faceIndices=e.getFaceIndices();
        for(int i=0;i<faceIndices.length;i++){
      public void faceDragEnd(FaceDragEvent e) {

Previous: Add a ready-made tool Developer Tutorial: Contents Next: Write a simple tool