1

I created an application which generates a board with a grid pattern, consisting of nodes which hold square objects in javaFX, using GridPanel. Below is the current output:

enter image description here

I want to know how to return the coordinate of a node, after CLICKING on the node. I am aware I have to use an action listener of sorts, but I'm not entirely familiar when it comes to having node coordinates.

Below is the current source code, thank you very much.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class MainApp extends Application {

    private final double windowWidth = 1000;
    private final double windowHeight = 1000;

    /*n is amount of cells per row
      m is amount of cells per column*/
    private final int n = 50;
    private final int m = 50;

    double gridWidth = windowWidth / n;
    double gridHeight = windowHeight / m;

    MyNode[][] playfield = new MyNode[n][m];

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        // initialize playfield
        for( int i=0; i < n; i++) {
            for( int j=0; j < m; j++) {

                // create node
                MyNode node = new MyNode( i * gridWidth, j * gridHeight, gridWidth, gridHeight);

                // add node to group
                root.getChildren().add( node);

                // add to playfield for further reference using an array
                playfield[i][j] = node;

            }
        }

        Scene scene = new Scene( root, windowWidth, windowHeight);

        primaryStage.setScene( scene);
        primaryStage.show();
        primaryStage.setResizable(false);
        primaryStage.sizeToScene();
    }

    public static class MyNode extends StackPane {

        public MyNode(double x, double y, double width, double height) {

            // create rectangle
            Rectangle rectangle = new Rectangle( width, height);
            rectangle.setStroke(Color.BLACK);
            rectangle.setFill(Color.LIGHTGREEN);

            // set position
            setTranslateX(x);
            setTranslateY(y);

            getChildren().addAll(rectangle);
        }
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    If you set the `onMouseClicked` handler you can get the coordinates, either directly or by querying the `MouseEvent`. – Slaw Feb 24 '19 at 23:41
  • https://github.com/sedj601/ConwaysGameOfLife/tree/master/src/conwaysgameoflife – SedJ601 Feb 25 '19 at 05:33

1 Answers1

1

You can add mouse event handler to root :

  root.setOnMousePressed(e->mousePressedOnRoot(e));

Where mousePressedOnRoot(e) is defined as

  private void mousePressedOnRoot(MouseEvent e) {
    System.out.println("mouse pressed on (x-y): "+e.getSceneX()+"-"+e.getSceneY());
  }

Edit: alternatively you can add mouse event handler to each MyNode instance by adding setOnMousePressed(e->mousePressedOnNode(e)); to its constructor.

and add the method:

 private void mousePressedOnNode(MouseEvent e) {
        System.out.println("mouse pressed on (x-y): "+e.getSceneX()+"-"+e.getSceneY());
 }

If you need the coordinates within the clicked node use e.getX() and e.getY()

c0der
  • 18,467
  • 6
  • 33
  • 65
  • This code works, but it is returning the coordinates of the actual screen instead of the node that im currently selecting. – Ahmad Mohammad Feb 25 '19 at 12:37
  • See my edit. I am not sure what exactly you need, you may want to read [this](https://stackoverflow.com/questions/31148690/get-real-position-of-a-node-in-javafx) – c0der Feb 25 '19 at 13:03