简体   繁体   English

在拖动Javafx上移动形状-形状不断重置

[英]Moving Shape on Drag Javafx - Shape Keeps Resetting

I'm trying to get a simple rectangle to move on drag. 我正在尝试获得一个简单的矩形以继续拖动。 I have it moving, but when I try to drag again, it first reverts back to the initial position before the initial drag. 我移动了它,但是当我尝试再次拖动时,它会先恢复到初始位置,然后再进行初始拖动。 I've tried using StackPane, FlowPane, Pane, and AnchorPane. 我试过使用StackPane,FlowPane,Pane和AnchorPane。

  public View getView() {
        View view = new View("Test View");
        view.setName(name);
        Pane pane = new Pane();

        rect.setOnMousePressed((MouseEvent me) -> {
            xVal = me.getSceneX();
            yVal = me.getSceneY();
            System.out.println("PRESSED_" + Double.toString(rect.getX()));
        });

        rect.setOnMouseReleased((MouseEvent me) -> {
            System.out.println("RELEASE_" + Double.toString(rect.getX()));
        });

        rect.setOnMouseDragged((MouseEvent me) ->{
            final double diffX = me.getSceneX() - xVal;
            final double diffY = me.getSceneY() - yVal;
            System.out.println("SET_" + Double.toString(rect.getX()));
            rect.setX(diffX);
            rect.setY(diffY);
        });

        pane.getChildren().add(rect);
        view.setCenter(pane);
        return view;
    }

Note: rect , xVal , yVal are all class properties. 注意: rectxValyVal均为类属性。

The issue again is that I can move the rectangle just fine. 再次的问题是我可以很好地移动矩形。 However when I try to move again, the rectangle seems to jump back to the original point of the rectangle. 但是,当我尝试再次移动时,矩形似乎跳回到了矩形的原始点。 You can see in the following image where it seems to revert: 您可以在下图中看到它似乎在还原:

矩形拖曳问题

Note: The fact that the diffX and diffY are final isn't a problem here. 注意: diffXdiffYfinal的事实在这里不是问题。

Another Note: I have also tried setTranslate() as well as getTransforms().add() ... 另一个注意事项:我也尝试过setTranslate()getTransforms().add() ...

OK so I was fiddling around with it and I figured it out that every time I press down, the X and Y coordinates of the rectangle are calibrated at (0,0) . 好的,所以我随便摆弄它,发现每次按下时,矩形的X和Y坐标都校准为(0,0) So all I had to do was to set xVal and yVal to 0 in the initial mouse press event. 因此,我要做的就是在最初的鼠标按下事件中将xValyVal设置为0。 However, this makes xVal and yVal to be trivial. 但是,这使xValyVal变得微不足道。 Thus, they can be deleted. 因此,可以将其删除。 Not really sure if this was the best solution, but it works for me: 不太确定这是否是最佳解决方案,但对我有用:

    rect.setOnMousePressed((MouseEvent me) -> {
        System.out.println("PRESSED_" + Double.toString(rect.getX()));
    });

    rect.setOnMouseReleased((MouseEvent me) -> {
        System.out.println("RELEASE_" + Double.toString(rect.getX()));
    });

    rect.setOnMouseDragged((MouseEvent me) ->{
        double diffX = me.getX() - rect.getWidth() / 2;
        double diffY = me.getY() - rect.getHeight() / 2;
        System.out.println("SET_" + Double.toString(rect.getX()));
        rect.setX(diffX);
        rect.setY(diffY);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM