Example usage for javafx.scene.shape Rectangle setOnTouchMoved

List of usage examples for javafx.scene.shape Rectangle setOnTouchMoved

Introduction

In this page you can find the example usage for javafx.scene.shape Rectangle setOnTouchMoved.

Prototype

public final void setOnTouchMoved(EventHandler<? super TouchEvent> value) 

Source Link

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);

    final Rectangle rect = new Rectangle();
    rect.setWidth(100);/*from   w ww  .  j a  v  a 2 s .co  m*/
    rect.setHeight(100);
    root.getChildren().add(rect);

    rect.setOnTouchPressed(new EventHandler<TouchEvent>() {
        @Override
        public void handle(TouchEvent event) {
            if (touchId == -1) {
                touchId = event.getTouchPoint().getId();
                touchx = event.getTouchPoint().getSceneX() - rect.getTranslateX();
                touchy = event.getTouchPoint().getSceneY() - rect.getTranslateY();
            }
            event.consume();
        }
    });

    rect.setOnTouchReleased(new EventHandler<TouchEvent>() {
        @Override
        public void handle(TouchEvent event) {
            if (event.getTouchPoint().getId() == touchId) {
                touchId = -1;
            }
            event.consume();
        }
    });

    rect.setOnTouchMoved(new EventHandler<TouchEvent>() {
        @Override
        public void handle(TouchEvent event) {
            if (event.getTouchPoint().getId() == touchId) {
                rect.setTranslateX(event.getTouchPoint().getSceneX() - touchx);
                rect.setTranslateY(event.getTouchPoint().getSceneY() - touchy);
            }
            event.consume();
        }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}