0

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.

Community
  • 1
  • 1
Chexxor
  • 406
  • 6
  • 17
  • One question and one (maybe incidental) suggestion. Why do you need the mouse coordinates like this? (Perhaps there is an easier way to achieve what you're trying to achieve.) And the suggestion: your rectangle + text + StackPane are really just the same as a label (maybe with some style added to it using CSS). It would probably simplify your code to just use a label. – James_D Mar 22 '16 at 16:50
  • And when you say your are looking for the buttons' positions, you want them relative to what? The border pane? The scene? The screen? – James_D Mar 22 '16 at 16:53
  • @James_D Relative to scene, thus I use localToScene for the bounds. And will label help me with the case? I made my button class by extending StackPane and adding rectangle and text to it and some other methods I need. For instance I'm changing the fill and stroke of the rectangle with a .hover() method. And that's why I need to know the position since I'm hard-coding the code for mouse interaction. – Chexxor Mar 22 '16 at 16:56
  • For mouse "interaction", normally you would just register mouse listeners with the "button" (i.e. pane, or label, either way). Then you don't need any of this: the event is fired if it occurs when the mouse is in the bounds of the button. At a minimum, using a label will simplify both the code and the layout (replacing three components with just one). I assume you are executing all this code after layout has been performed...? Also, the only problem seems to be that you have a zero x-coordinate. Maybe this is "real". Set a background on `button1` to see where it is in the layout. – James_D Mar 22 '16 at 16:59
  • As for changing the fill and stroke, you would normally just do that with CSS (`.label { ...}` and `.label:hover { ... }`). It just seems like you are trying to recreate lots of functionality that is already part of JavaFX. – James_D Mar 22 '16 at 17:04
  • Well I know I'm inventing the wheel over again but I do it for learning purposes. And the reason I don't use listeners is because I already have an event handler that I intended to add this too. I'm gonna check the background now. – Chexxor Mar 22 '16 at 17:12
  • IMHO all you are learning is details of how not to use JavaFX.. but OK. An event handler is a listener though, that's what I meant. The point is you can add an event handler (listener) to the buttons themselves, then you don't need to do all this computation with the coordinates. – James_D Mar 22 '16 at 17:14
  • Aha, I thought I could only do that with button nodes... But guess what! Setting the Pane's width to match the rectangle's with solved my issue! Thank you so much, if u create an answer I'll give u solution mark. – Chexxor Mar 22 '16 at 17:15

1 Answers1

0

My issue was cause by the VBox and my StackPanes taking up all the possible space, not just the area of the visible Rectangle. Problem was solved by calling setMaxWidth(100) on the StackPane which contained the Rectangle and the Text.

Also, the only problem seems to be that you have a zero x-coordinate. Maybe this is "real". Set a background on button1 to see where it is in the layout. – James_D

Chexxor
  • 406
  • 6
  • 17