gpt4 book ai didi

javafx-2 - JavaFX2 : Drag Event start and end coordinates

转载 作者:行者123 更新时间:2023-12-02 01:57:31 25 4
gpt4 key购买 nike

我有一个显示图像的节点(ImageView)和位于其顶部的另一个节点(矩形)。我需要的行为是,当鼠标在矩形上拖动(按下-拖动-释放手势)时,两个节点都应该连贯地移动。我的思考过程朝着以下方向发展:

• 沿拖动方向将两个节点移动相同的距离。

对于此选项(也许还有更多选项),我需要鼠标拖动开始和结束之间的距离。我 try catch 拖动的开始和结束坐标,但没有成功。我想我迷失在要实现的处理程序中。

我的代码如下:

import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

public class MyView
{

double mouseDragStartX;
double mouseDragEndX;

double mouseDragStartY;
double mouseDragEndY;

ImageView imageView;

public MyView()
{
imageView = new ImageView("C:\\temp\\test.png");
}

private void setRectangleEvents(final MyObject myObject)
{
myObject.getRectangle().setOnMousePressed(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
mouseDragStartX = mouseEvent.getX();
mouseDragStartY = mouseEvent.getY();
mouseEvent.consume();
}
});

myObject.getRectangle().setOnMouseReleased(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
mouseDragEndX = mouseEvent.getX();
mouseDragEndY = mouseEvent.getY();
myObjectDraggedHandler();
}
});
}

private void myObjectDraggedHandler()
{

Rectangle2D viewport = this.imageView.getViewport();
double newX = this.imageView.getImage().getWidth()
+ (mouseDragEndX - mouseDragStartX);
double newY = this.imageView.getImage().getHeight()
+ (mouseDragEndY - mouseDragStartY);

this.imageView.setViewport(new Rectangle2D(newX, newY, viewport.getWidth(), viewport
.getHeight()));

}
}

P.S:此代码仅用于指示我要实现的内容,并且无法正确编译。

或者也许我的问题应该是:
如何捕获鼠标拖动的长度或跨度?

最佳答案

试试这个我认为它会帮助你

obj.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = stage.getX() - mouseEvent.getScreenX();
dragDelta.y = stage.getY() - mouseEvent.getScreenY();
}
});
obj.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
stage.setX(mouseEvent.getScreenX() + dragDelta.x);
stage.setY(mouseEvent.getScreenY() + dragDelta.y);
}
});

关于javafx-2 - JavaFX2 : Drag Event start and end coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com