Example usage for javafx.scene.input TouchEvent getTouchPoint

List of usage examples for javafx.scene.input TouchEvent getTouchPoint

Introduction

In this page you can find the example usage for javafx.scene.input TouchEvent getTouchPoint.

Prototype

public TouchPoint getTouchPoint() 

Source Link

Document

Gets the touch point of this event.

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);/*  ww  w  . ja  v a2 s .  c  o 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();
}