I am remaking a Boardgame in java. I wanted to be able to click on a token, then on a spot, and have that token move to that spot. But, the token moves to a complete different location.
On the image, I clicked in the red square, but the token ended up to the right and down from the actual spot I clicked. (The token is the round thing with the stark logo on it)
I printed the coördinates of the clicks and the location to see if they would differ, but this was not the case.
The code of the clicking part:
map.setOnMousePressed((e -> {
System.out.println(e.getX());
System.out.println(e.getY());
currentOrder.setX(e.getX());
currentOrder.setY(e.getY());
System.out.println(currentOrder.getX());
System.out.println(currentOrder.getY());
currentOrder = basics;
}));
The output of coördinates:
564.0
365.3333333333333
564.0
365.3333333333333
610.0
267.3333333333333
610.0
267.3333333333333
569.3333333333334
319.3333333333333
569.3333333333334
319.3333333333333
As you can see the coördinates of the clicked spot and the coördinates of the token are the same. But, on the screen, they are still not in the same spot.
Why is this the case? And how could I fix this?
I tried subtracting the translateX from the one I got, I tried subtracting the width of the token from the coördinates, I tried getSceneX and getScreenX, I tried setLayoutX. And all of this in every possible combination. Neither of it seemed to work. Sometimes it even gave the token a fixed X position and only the Y changed. So, not anything that worked.
Questions I read but didn't help me:
- How to anchor text at its center in javafx?
- Get mouse position on node relative to it's coordinates and transforms
- How to get location of mouse in JavaFX?
And probably some others I can't find anymore.
I used this as a reference to make that part of my code.
I tried to add only the relevant code. If another part of my code is needed for clarification, please let me know.