gpt4 book ai didi

java - Javafx在特定条件下每10毫秒触发一个事件

转载 作者:行者123 更新时间:2023-11-30 07:05:42 24 4
gpt4 key购买 nike

您好,我试图在检测到拖动但鼠标尚未释放的情况下每 10 毫秒触发一个事件。下面是我想要触发的代码。

mapScroll.addEventFilter(MouseEvent.MOUSE_DRAGGED, e -> {
endX = e.getX();
endY = e.getY();
Bounds viewBounds = mapScroll.getViewportBounds();
double startEndX = startX - endX;
double startEndY = startY - endY;

Bounds contentBounds = mapScroll.getContent().getLayoutBounds();

double hChange = startEndX / (contentBounds.getWidth() - viewBounds.getWidth());
double vChange = startEndY / (contentBounds.getHeight() - viewBounds.getHeight());
mapScroll.setHvalue(mapScroll.getHvalue() + hChange);
mapScroll.setVvalue(mapScroll.getVvalue() + vChange);

startX = endX;
startY = endY;
});

不幸的是,在我尝试在事件中使用预定执行程序服务之前,但这无法正常工作。

最佳答案

只需添加检查自上次执行以来是否已过去 10 毫秒。 (我使用了 LongProperty,因为带有 Lambda 的 final restriction)

final LongProperty timeOfLastExecute = new SimpleLongProperty(System.currentTimeMillis());

mapScroll.addEventFilter(MouseEvent.MOUSE_DRAGGED, e -> {
if(System.currentTimeMillis() - timeOfLastExecute.get() > 10) {
endX = e.getX();
endY = e.getY();
Bounds viewBounds = mapScroll.getViewportBounds();
double startEndX = startX - endX;
double startEndY = startY - endY;

Bounds contentBounds = mapScroll.getContent().getLayoutBounds();

double hChange = startEndX / (contentBounds.getWidth() - viewBounds.getWidth());
double vChange = startEndY / (contentBounds.getHeight() - viewBounds.getHeight());
mapScroll.setHvalue(mapScroll.getHvalue() + hChange);
mapScroll.setVvalue(mapScroll.getVvalue() + vChange);

startX = endX;
startY = endY;
timeOfLastExecute.set(System.currentTimeMillis());
}
});

关于java - Javafx在特定条件下每10毫秒触发一个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40172644/

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