使用鼠标拖动对象

时间:2016-04-14 06:37:15

标签: java javafx

我正在尝试移动和旋转任何可见对象。但拖拽和旋转物体后,在旧的地方定义。

鼠标光标在对象上方时会发生变化。

告诉我我做错了什么?

public class Main extends Application {  
    Point2D offset;  
    Shape selected;  

    @Override  
        public void start(Stage primaryStage) {  
        Pane root = new Pane();  
        Rectangle rect = new Rectangle(40, 20, 50, 100);  
        rect.setRotate(90);  
        Ellipse ellipse = new Ellipse(450, 450, 50, 25);  
        ellipse.setRotate(45);  
        Circle circle = new Circle(250, 250, 50);  
        root.getChildren().addAll(rect, ellipse, circle);  

        root.setOnMousePressed(e-> {  
            offset = new Point2D(e.getX(), e.getY());  
            e.consume();  
        });  

        root.setOnMouseMoved(e-> {    
            for(Node node: root.getChildren()){  
                if(node.contains(e.getX(), e.getY())){  
                    selected = (Shape) node;  
                    root.setCursor(Cursor.HAND);  
                    break;  
                }else{  
                    selected = null;  
                    root.setCursor(Cursor.DEFAULT);  
                }  
            }  
            e.consume();  
        });  

        root.setOnMouseDragged(e-> {    
            if(selected != null){  
                selected.setTranslateX(e.getX() - offset.getX());  
                selected.setTranslateY(e.getY() - offset.getY());  
            }  
            e.consume();  
        });  

        Scene scene = new Scene(root,500,500);  
        primaryStage.setScene(scene);  
        primaryStage.show();  
    }  

    public static void main(String[] args) {  
        launch(args);  
    }  
}  

1 个答案:

答案 0 :(得分:0)

您未正确处理初始翻译。您需要在计算中包含这些值:

    在检查contains 中的onMouseMoved之前,
  • 从鼠标位置减去
  • 在计算offset
  • 时从鼠标位置减去

但是我建议为root的子项设置光标而不是在事件处理程序中执行此操作,也不要检查所点击的节点,而是使用target提供的MouseEvent

Point2D offset;
Node selected;
Point2D translateStart;

@Override
public void start(Stage primaryStage) {
    Pane root = new Pane();
    Rectangle rect = new Rectangle(40, 20, 50, 100);
    rect.setRotate(90);
    Ellipse ellipse = new Ellipse(450, 450, 50, 25);
    ellipse.setRotate(45);
    Circle circle = new Circle(250, 250, 50);
    root.getChildren().addAll(rect, ellipse, circle);

    rect.setCursor(Cursor.HAND);
    circle.setCursor(Cursor.HAND);
    ellipse.setCursor(Cursor.HAND);

    root.setOnMousePressed(e -> {
        Node target = (Node) e.getTarget();
        if (target != root) {
            // event occured on a child
            selected = target;
            offset = new Point2D(e.getX(), e.getY());
            translateStart = new Point2D(selected.getTranslateX(), selected.getTranslateY());
        } else {
            selected = null;
        }
        e.consume();
    });

    root.setOnMouseDragged(evt -> {
        if (selected != null) {
            selected.setTranslateX(evt.getX() - offset.getX() + translateStart.getX());
            selected.setTranslateY(evt.getY() - offset.getY() + translateStart.getY());
        }
        evt.consume();
    });

    Scene scene = new Scene(root, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}