Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8115927

onMouseDragged does not work when attached to a Group within a scene

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7u60
    • javafx
    • None
    • Windows XP, JavaFX 8 ea b97

      I would like to run JavaFX 3D programes from within a BorderPane. I have set up a BorderPane
      and put a 3D display in the middle.
      The object displays fine but I do not get Events from onMouseDragged when it is attached to
      the Group containing the 3D objects. It works fine when attached to the Scene.

      Example (change parent.setOnMouseDragged to scene.setOnMouseDragged)
      -------------
      package javafx3d;
      import javax.vecmath.Point3f;
      import javax.vecmath.TexCoord2f;
      import javax.vecmath.Tuple2f;
      import javax.vecmath.Tuple3f;

      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.PerspectiveCamera;
      import javafx.scene.PointLight;
      import javafx.scene.Scene;
      import javafx.scene.canvas.Canvas;
      import javafx.scene.canvas.GraphicsContext;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.paint.Color;
      import javafx.scene.paint.PhongMaterial;
      import javafx.scene.shape.MeshView;
      import javafx.scene.shape.Rectangle;
      import javafx.scene.shape.TriangleMesh;
      import javafx.scene.transform.Rotate;
      import javafx.stage.Stage;
       
      public class SmoothCylinder4 extends Application {
       
        double anchorX, anchorY, anchorAngle;
        Scene scene;
       
        private PerspectiveCamera addCamera(Scene scene) {
          PerspectiveCamera perspectiveCamera = new PerspectiveCamera(false);
          scene.setCamera(perspectiveCamera);
          return perspectiveCamera;
        }
       
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
          launch(args);
        }
       
        @Override
        public void start(Stage stage) {
          BorderPane bpane;
          Scene scene;
          Canvas tc,lc,cc,rc,bc;
          GraphicsContext tgc,lgc,cgc,rgc,bgc;
          Double w,h;
          Double wt,ht;
          Group root;
          
          stage.setTitle("Smooth Cylinder and Cone");

          bpane = new BorderPane();
          w = 300.;
          h = 300.;
          ht = h/3;
          wt = w/3;
          tc = new Canvas(w,ht);
          tgc = tc.getGraphicsContext2D();
          tgc.setFill(Color.BLUE);
          tgc.fillRect(0,0,w,ht);
          lc = new Canvas(wt,ht);
          lgc = lc.getGraphicsContext2D();
          lgc.setFill(Color.RED);
          lgc.fillRect(0,0,wt,ht);
          cc = new Canvas(wt,ht);
          cgc = cc.getGraphicsContext2D();
          cgc.setFill(Color.ORANGE);
          cgc.fillRect(0,0,wt,ht);
          rc = new Canvas(100,100);
          rgc = rc.getGraphicsContext2D();
          rgc.setFill(Color.YELLOW);
          rgc.fillRect(0,0,wt,ht);
          bc = new Canvas(w,ht);
          bgc = bc.getGraphicsContext2D();
          bgc.setFill(Color.GREEN);
          bgc.fillRect(0,0,w,ht);
          bpane.setTop(tc);
          bpane.setLeft(lc);
      // bpane.setCenter(cc);
          bpane.setRight(rc);
          bpane.setBottom(bc);

          PhongMaterial redMaterial = new PhongMaterial();
          redMaterial.setSpecularColor(Color.ORANGE);
          redMaterial.setDiffuseColor(Color.RED);
       
          Cylinder cylinder = new Cylinder();
          MeshView red = cylinder.build(25f,37.5f,101);
          red.setMaterial(redMaterial);
          red.setTranslateX(30);
          red.setTranslateY(30);
          red.setTranslateZ(56);
          
          PhongMaterial yellowMaterial = new PhongMaterial();
          yellowMaterial.setDiffuseColor(Color.YELLOW);
          yellowMaterial.setSpecularColor(Color.LIGHTYELLOW);
          Cone cone = new Cone();
          MeshView yellow = cone.build(50f,75f,101);
          yellow.setMaterial(yellowMaterial);
          yellow.setTranslateX(30);
          yellow.setTranslateY(30);
          yellow.setTranslateZ(6);

          final Group parent = new Group(red,yellow);
          parent.setTranslateZ(125);
          parent.setRotationAxis(Rotate.Y_AXIS);
          Rectangle r = new Rectangle(60,60,Color.BROWN);
          parent.getChildren().add(r);
         
      // root = new Group(parent);
          bpane.setCenter(parent);
          root = new Group(bpane);
       
          scene = new Scene(root, 300, 300, true);
          scene.setFill(Color.PURPLE);
          
          parent.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent event) {
              parent.setRotate(anchorAngle + anchorX - event.getSceneX());
      // System.out.println(event.getX()+","+event.getY());
      // System.out.println(parent.getWidth()+","+parent.getHeight());
                    }
          });
       
          PointLight pointLight = new PointLight(Color.ANTIQUEWHITE);
          pointLight.setTranslateX(-500);
          pointLight.setTranslateY(-10);
          pointLight.setTranslateZ(-500);
       
          root.getChildren().add(pointLight);
       
          addCamera(scene);
          stage.setScene(scene);
          stage.show();
        }
        public class Cylinder {
          public MeshView build(float r,float h,int pts) {
            TriangleMesh tm = new TriangleMesh();
            MeshView mv = new MeshView(tm);
            int[] idxa;
            int[] fsg;
            float[] txs;
            Point3f[] tcir,bcir;
            Point3f[] cyl;
            float[] cps;
            int[] idx = {0,0,1,1,0,1};
            TexCoord2f[] tcd;
            int i,j,n;
            boolean diag = false;
      /*
       * build points on cylinder
       */
            RotRtns rr = new RotRtns();
            tcir = rr.circle(r,h/2f,pts+1);
            bcir = rr.circle(r,-h/2f,pts+1);
            cyl = new Point3f[pts*6];
            n = 0;
            for (i = 0;i < pts;i++) {
              cyl[n++] = bcir[idx[0]+i];
              cyl[n++] = tcir[idx[1]+i];
              cyl[n++] = bcir[idx[2]+i];
              cyl[n++] = bcir[idx[3]+i];
              cyl[n++] = tcir[idx[4]+i];
              cyl[n++] = tcir[idx[5]+i];
            }
      /*
       * build texture points for cylinder
       */
            tcd = texcds(r,h,pts);
      /*
       * build arrays for mesh
       */
            n = pts * 2;
            idxa = new int[n*6];
            j = 0;
            for (i = 0;i < n*6;i+=2) { // face indices
              idxa[i] = j; idxa[i+1] = j++;
            }
            fsg = new int[n];
            for (i = 0;i < n;i++) fsg[i] = 1; // face smoothing
            VecMathPlus vmp = new VecMathPlus();
            cps = vmp.Tuple3f2float(cyl); // vertices
            txs = vmp.Tuple2f2float(tcd); // texcoords
      /*
       * setPoints
       */
            if (diag) {
              System.out.println("coordinates = "+cps.length);
              for (i = 0;i < cps.length;i+=3) {
                for (j = 0;j < 3;j++) {
                  System.out.print(cps[i+j]+",");
                }
                System.out.println();
              }
            }
            tm.getPoints().setAll(cps);
      /*
       * setTexCoords
       */
            if (diag) {
              System.out.println("texcoords = "+txs.length);
              for (i = 0;i < txs.length;i+=2) {
                for (j = 0;j < 2;j++) {
                  System.out.print(txs[i+j]+",");
                }
                System.out.println();
              }
            }
            tm.getTexCoords().setAll(txs);
      /*
       * setFaces
       */
            if (diag) {
              System.out.println("faces = "+idxa.length);
              for (i = 0;i < idxa.length;i++) {
                System.out.print(idxa[i]+",");
              }
              System.out.println();
            }
            tm.getFaces().setAll(idxa);
      /*
       * setSmoothing
       */
            if (diag) {
              System.out.println("fsgs = "+fsg.length);
              for (i = 0;i < fsg.length;i++) {
                System.out.print(fsg[i]+",");
              }
              System.out.println();
            }
            tm.getFaceSmoothingGroups().setAll(fsg);

            return(mv);
          }
          TexCoord2f[] texcds(float r,float h,int pts) {
            TexCoord2f[] tcd;
            float a,b;
            int i,n;
            tcd = new TexCoord2f[pts*6];
            a = (r+r) * 3.14159f / (float) pts;
            n = 0;
            for (i = 0;i < pts;i++) {
              b = a * (float) i;
              tcd[n++] = new TexCoord2f(b,-h/2f);
              tcd[n++] = new TexCoord2f(b,h/2f);
              tcd[n++] = new TexCoord2f(b+a,-h/2f);
              tcd[n++] = new TexCoord2f(b+a,-h/2f);
              tcd[n++] = new TexCoord2f(b,h/2f);
              tcd[n++] = new TexCoord2f(b+a,h/2f);
            }
            for (i = 0;i < tcd.length;i++) tcd[i].scale(.005f);
            return(tcd);
          }
        }
        public class Cone {
          public MeshView build(float r,float h,int pts) {
            TriangleMesh tm = new TriangleMesh();
            MeshView mv = new MeshView(tm);
            int[] idxa;
            int[] fsg;
            float[] txs;
            Point3f[] bcir;
            Point3f[] con;
            Point3f top;
            float[] cps;
      // int[] idx = {0,0,1,1,0,1};
            TexCoord2f[] tcd;
            int i,j,n;
            boolean diag = false;
      /*
       * build points on cone
       */
            RotRtns rr = new RotRtns();
            bcir = rr.circle(r,h/2f,pts+1);
            con = new Point3f[pts*3];
            n = 0;
            top = new Point3f(0,-h/2f,0);
            for (i = 0;i < pts;i++) {
              con[n++] = top;
              con[n++] = bcir[i];
              con[n++] = bcir[i+1];
            }
      /*
       * build texture points for cone
       */
            tcd = texcds(r,h,pts);
      /*
       * build arrays for mesh
       */
            n = pts * 2;
            idxa = new int[n*3];
            j = 0;
            for (i = 0;i < n*3;i+=2) { // face indices
              idxa[i] = j; idxa[i+1] = j++;
            }
            fsg = new int[pts];
            for (i = 0;i < pts;i++) fsg[i] = 1; // face smoothing
            VecMathPlus vmp = new VecMathPlus();
            cps = vmp.Tuple3f2float(con); // vertices
            txs = vmp.Tuple2f2float(tcd); // texcoords
      /*
       * setPoints
       */
            if (diag) {
              System.out.println("coordinates = "+cps.length);
              for (i = 0;i < cps.length;i+=3) {
                for (j = 0;j < 3;j++) {
                  System.out.print(cps[i+j]+",");
                }
                System.out.println();
              }
            }
            tm.getPoints().setAll(cps);
      /*
       * setTexCoords
       */
            if (diag) {
              System.out.println("texcoords = "+txs.length);
              for (i = 0;i < txs.length;i+=2) {
                for (j = 0;j < 2;j++) {
                  System.out.print(txs[i+j]+",");
                }
                System.out.println();
              }
            }
            tm.getTexCoords().setAll(txs);
      /*
       * setFaces
       */
            if (diag) {
              System.out.println("faces = "+idxa.length);
              for (i = 0;i < idxa.length;i++) {
                System.out.print(idxa[i]+",");
              }
              System.out.println();
            }
            tm.getFaces().setAll(idxa);
      /*
       * setSmoothing
       */
            if (diag) {
              System.out.println("fsgs = "+fsg.length);
              for (i = 0;i < fsg.length;i++) {
                System.out.print(fsg[i]+",");
              }
              System.out.println();
            }
            tm.getFaceSmoothingGroups().setAll(fsg);

            return(mv);
          }
          TexCoord2f[] texcds(float r,float h,int pts) {
            TexCoord2f[] tcd;
            float a,b,c;
            int i,n;
            tcd = new TexCoord2f[pts*3];
            a = (r+r) * 3.14159f / (float) pts;
            c = a * 0.5f;
            n = 0;
            for (i = 0;i < pts;i++) {
              b = a * (float) i;
              tcd[n++] = new TexCoord2f(b+c,h/2f);
              tcd[n++] = new TexCoord2f(b,-h/2f);
              tcd[n++] = new TexCoord2f(b+a,-h/2f);
            }
            for (i = 0;i < tcd.length;i++) tcd[i].scale(.005f);
            return(tcd);
          }
        }
        public class VecMathPlus {
          public float[] Tuple3f2float(Tuple3f[] p3f) {
            float[] pts;
            int i,k,n;
          
            k = p3f.length;
            pts = new float[k*3];
            n = 0;
            for (i = 0;i < k;i++){
              pts[n++] = p3f[i].x;
              pts[n++] = p3f[i].y;
              pts[n++] = p3f[i].z;
            }
            return(pts);
          }
          public float[] Tuple2f2float(Tuple2f[] p3f) {
            float[] pts;
            int i,k,n;
          
            k = p3f.length;
            pts = new float[k*2];
            n = 0;
            for (i = 0;i < k;i++){
              pts[n++] = p3f[i].x;
              pts[n++] = p3f[i].y;
            }
            return(pts);
          }
        }
        public class RotRtns {
          public Point3f[] circle(float r,float h,int pts) {
            Point3f[] cir;
            double fts;
            double tpi;
            double a;
            int i;
      /*
       * built points on circle
       */
            cir = new Point3f[pts];
            fts = (pts - 1);
            tpi = Math.PI * 2.0;
            for (i = 0;i < (pts-1);i++) {
              a = tpi * (double) i / fts;
              cir[i] = new Point3f();
              cir[i].x = r * (float) Math.cos(a);
              cir[i].z = r * (float) Math.sin(a);
              cir[i].y = h;
            }
            cir[pts-1] = cir[0];
            return(cir);
          }
        }
      }

            psafrata Pavel Ĺ afrata
            dscalesjfx Don Scales (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: