0

How can I know in what direction the mouse is moving? I need to know if the mouse moves to the left, right, bottom or top of the scene.

Thanks

Jens Vde
  • 43
  • 5
  • Look at the change in X to know if the mouse is moving left or right. Look at the change in Y to know if the mouse is moving up or down. If the change in X is positive, the mouse is moving down. If the change in Y is positive, the mouse is moving right. – SedJ601 Feb 23 '18 at 14:48
  • I screwed up on the second half of that comment above. ChangeInX > 0 -> moving right. ChangeInX < 0 -> moving left. ChangeInY > 0 -> moving down. ChangeInY < 0 -> moving up. Sorry. – SedJ601 Feb 23 '18 at 15:06

2 Answers2

1

You can add an event handler in the gridpane to record the location of mouse when it is moved. Then compare the new location to the old one to figure out which direction the mouse is moving.

int x, y;

gridpane.addEventHandler(MouseEvent.MOUSE_MOVED, e ->{
    if (e.getX() < x) {
        // left
    } else if (e.getX() > x) {
        // right
    } else if (e.getY() < y) {
        // up
    } else if (e.getY() > y) {
        // down
    }
    x = e.getX();
    y = e.getY();
});
Tom Dee
  • 2,516
  • 4
  • 17
  • 25
1

Here is a sample add that demos the idea. Look at the change in X to know if the mouse is moving left or right. Look at the change in Y to know if the mouse is moving up or down. If ChangeInX > 0 -> moving right. If ChangeInX < 0 -> moving left. If ChangeInY > 0 -> moving down. If ChangeInY < 0 -> moving up.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class MouseDirectionTest extends Application
{

    double orgX, orgY;

    @Override
    public void start(Stage primaryStage)
    {

        StackPane root = new StackPane();
        root.setOnMouseEntered((event) -> {
            orgX = event.getSceneX();
            orgY = event.getSceneY();
        });
        root.setOnMouseMoved((event) -> {
            double changeInX = event.getSceneX() - orgX;
            double changeInY = event.getSceneY() - orgY;
            //System.out.println(orgX / orgY);

            if (changeInX > 0) {
                System.out.println("moving right");
            }
            else if (changeInX < 0) {
                System.out.println("moving left");
            }
            if (changeInY > 0) {
                System.out.println("moving down");
            }
            else if (changeInY < 0) {
                System.out.println("moving up");
            }

            orgX = event.getSceneX();
            orgY = event.getSceneY();
        });

        Scene scene = new Scene(root, 700, 500);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59