Example usage for javafx.scene.input TouchEvent consume

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

Introduction

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

Prototype

public void consume() 

Source Link

Document

Marks this Event as consumed.

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  w  w  .  j av  a  2s. 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();
}