Results 1 to 2 of 2
  1. #1
    klaus.willson is offline Member
    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0

    Default JavaFX Problem while dragging and scaling

    Hello i have a problem to understand and use getX, getY, getSceneX, getSceneY, getScreenX, getScreenY
    and also convert parentToScene, sceneToLocal ...

    I have a simple scene with a root pane (blue in my example) and a scale pane (green in my example)
    as children of root pane with shapes/nodes (black in my example) in the scale pane.

    The scale pane can scale and drag and also can drag nodes/shapes in the scale pane.

    The first strange is when i scale out the root pane the mouse events not fire on the scene. :(
    So i must build a root pane and a chlidren in root as scale pane.
    This is a solution for the mouse events but i do not know if it is right.

    The major problem is :

    When i use the drag listener in the blue root pane the scale pane is not move right!
    In my example in the comment //work for scalepane and shape dragging but not when scaling
    The problem whe i drag the root and drag shapes and drag scalepane a fine ... but when i scale in
    the shapes not correctyl move to the mous cursor.

    i hope anybody can help me

    Her is my example code
    Java Code:
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Point2D;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.input.ScrollEvent;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.CubicCurve;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    public class DragProblem extends Application {
    
      Point2D start;
      Point2D drag;
    
      private class ScaleHandler implements EventHandler<ScrollEvent> {
    
        final Node n;
        double     range = 0.88d;;
    
        public ScaleHandler(Node n) {
          super();
          this.n = n;
        }
    
        @Override
        public void handle(ScrollEvent scrollEvent) {
          if(scrollEvent.isControlDown()) {
            double factor = (scrollEvent.getDeltaY() < 0) ? range : 1 / range;
            n.setScaleX(n.getScaleX() * factor);
            n.setScaleY(n.getScaleY() * factor);
            scrollEvent.consume();
          }
        }
    
      }
    
      private class DragAndDropHandler implements EventHandler<MouseEvent> {
    
        final Node n;
    
        private DragAndDropHandler(Node n) {
          super();
          this.n = n;
    
        }
    
        @Override
        public void handle(MouseEvent me) {
          if(me.getEventType() == MouseEvent.MOUSE_PRESSED) {
            start = new Point2D(n.getTranslateX(), n.getTranslateY());
            drag = n.localToParent(me.getX(), me.getY());
    
            // work for scalepane and shape dragging but not when scaling
            // drag = new Point2D(me.getSceneX(), me.getSceneY());
          }
          else if(me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            Point2D moveXyPoint = n.localToParent(me.getX(), me.getY());
    
            // work for scalepane and shape dragging but not when scaling
            // moveXyPoint = new Point2D(me.getSceneX(), me.getSceneY());
    
            double newX = moveXyPoint.getX() - drag.getX() + start.getX();
            double newY = moveXyPoint.getY() - drag.getY() + start.getY();
    
            n.setTranslateX(newX);
            n.setTranslateY(newY);
    
          }
          me.consume();
        }
    
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    
      @Override
      public void start(Stage primaryStage) {
    
        Pane scalePane = new Pane();
        scalePane.setStyle("-fx-background-color: green;");
        // add the scale handler
        addScaleHandler(scalePane);
        // also add the drag handler to pane
        addDragHandler(scalePane);
    
        Pane root = new Pane(scalePane);
        root.onMousePressedProperty().bind(scalePane.onMousePressedProperty());
        root.onMouseDraggedProperty().bind(scalePane.onMouseDraggedProperty());
        root.setStyle("-fx-background-color: blue;");
    
        Rectangle r1 = new Rectangle(100, 100, 50, 50);
        // add the drag handler to shape r1
        addDragHandler(r1);
        Rectangle r2 = new Rectangle(300, 300, 50, 50);
        // add the drag handler to shape r2
        addDragHandler(r2);
    
        CubicCurve curv = new CubicCurve(125, 450, 125, 450, 225, 500, 225, 500);
        curv.setStroke(Color.BLACK);
        curv.setStrokeWidth(1);
        curv.setFill(null);
        // add the drag handler to shape curv
        addDragHandler(curv);
    
        scalePane.getChildren().addAll(r1, r2, curv);
    
        // build stage
        primaryStage.setScene(new Scene(root, 1200, 800));
        primaryStage.show();
      }
    
      private void addScaleHandler(Node n) {
        ScaleHandler scaleEvent = new ScaleHandler(n);
        n.setOnScroll(scaleEvent);
      }
    
      private void addDragHandler(Node n) {
        DragAndDropHandler dndEvent = new DragAndDropHandler(n);
        n.setOnMousePressed(dndEvent);
        n.setOnMouseDragged(dndEvent);
      }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFX Problem while dragging and scaling

    "I have a simple scene with a root pane (blue in my example) and a scale pane (green in my example)
    as children of root pane with shapes/nodes (black in my example) in the scale pane. "

    You have a scene and it contains a root. The root is the scalePane according to line 92. The scalePane, the squares and the line are nodes. Suggestion: increase the stroke width in line 106 to 5 or more pixels for playing around with it. This way it is easier to grab.

    "The first strange is when i scale out the root pane the mouse events not fire on the scene. :("

    you hooked up the ScaleHandler to your scalePane (only). Scaling works on my machine for scalePane and its children. From what I know you can not scale the scene itself, the ScaleHandler only takes nodes for scaling.

    "When i use the drag listener in the blue root pane the scale pane is not move right!"

    I suggest to comment out lines 93 and 94 and play with the result. The scene is not a node. You can only drag nodes.

    "In my example in the comment //work for scalepane and shape dragging but not when scaling"

    Not sure what you are asking here, but scaling also only works for nodes. Scaling works on my machine.

    "The problem whe i drag the root and drag shapes and drag scalepane a fine ... but when i scale in
    the shapes not correctyl move to the mous cursor. "

    Scaling and dragging worked fine on my machine and as expected. Only the scalePane is hooked up to a ScaleHandler.

    I guess, 99% here is correct, there is just a little confusion about scenes and nodes. The Scene is a portion of a Stage (formerly "Window") that shows what you tell it to show. The Nodes are objects on that Scene and can be manipulated.

Similar Threads

  1. Replies: 5
    Last Post: 11-11-2015, 12:16 PM
  2. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  3. Zooming and scaling problem
    By Koopa in forum Java 2D
    Replies: 1
    Last Post: 07-07-2011, 10:30 PM
  4. The problem with scaling images
    By LianaN in forum AWT / Swing
    Replies: 3
    Last Post: 01-18-2011, 08:52 PM
  5. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •