I have a set of custom made "buttons" for the menu screen of my game. It's basically a StackPane
with a Rectangle
and a Text
node stacked. Basically this is similar to how my buttons are structured.
Pane button1 = new StackPane(new Rectangle(100, 50), new Text("Play!"));
Pane button2 = [....];
Then I insert buttons into a VBox and into my menu along with a header text:
VBox buttons = new VBox(button1, button2, button3...);
Pane menuScreen = new BorderPane(buttons, new Text("The Game"), null, null, null);
However, for my custom detection for mouse position compared to the buttons I need to know the buttons' positions...
int x = button1.getLayoutX(); //returns 0
int x = button1.getTranslateX(); //returns 0
int x = button1.localToScene(0, 0).getX(); //returns 0
int x = button1.localToScene(buttons.get(0).getBoundsInLocal()).getMinX(); //returns 0
int x = button1.localToScene(buttons.get(0).getBoundsInLocal()).getMaxX(); //returns the width of the entire scene
int x = buttons.get(0).getTranslateX(); //returns 0
int y = button1.localToScene(buttons.get(0).getBoundsInLocal()).getMinY();
The last case returns 234.0 if I have the VBox set with .setAlignment(Pos.CENTER)
and 64.0 if I don't. But for .getMinX()
it stays at 0.0 in either case. I believe it's related to the BorderPane
's left/right/bot regions being set to null
while the top region has the title text.
I cannot find any way of getting the x
coordinate when the buttons are in a layout pane other than the Pane
class itself. I tried StackPane as well. My suspicion is that there is no fixed coordinate and that properties are involved, but I only get confused from reading about it when I don't know what I'm looking for.
I have tried solutions from this quesion and this seems to be the same but as I mentioned I'm afraid my minX()
doesn't have a set value since the VBox
is the only thing filling the center row of my BorderPane
.
Edit: StackPane seems to give the right values for .getMinX() when I use .setAlignment(Pos.CENTER)
, but I am not allowed to do that with the text, only with the VBox, so then the text gets stuck on top of the buttons.