在特定条件Javafx下每10毫秒发射一次事件

时间:2016-10-21 09:11:32

标签: java events javafx mouseevent

您好我试图在检测到拖动的条件下每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;
    });

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

2 个答案:

答案 0 :(得分:1)

只需添加一个检查,如果自上次执行以来已经过了10毫秒。 (我使用LongProperty,因为final restriction和Lambdas)

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());
        }
    });

答案 1 :(得分:0)

您可以使用AnimationTimer(尝试)以每秒60帧的速度运行。所以每经过10秒就执行一次代码(每次都必须启动AnimationTimer):< / p>

<强>代码:

public void startTimer(){

   AnimationTimer timer = new AnimationTimer() {
           int seconds;
        @Override
        public void handle(long now) {

               //every time 10 seconds have passed
               //note that 1 second=1000 milliseconds=1.000.000 nanoseconds
               //now->variable which represents nano seconds
               if(now%10000000 == 0){

                  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;

              }
            }
        }
    }

     timer.start();

}