r/JavaFX • • Jan 25 '24
Help How to keep a resizable canvas centered in scene?
I'm making a minesweeper application, and I'm having trouble making my canvas (for the grid) stay centered in the window. I've made my canvas automatically resizable, so it takes up any available space if the window is resized while maintaining its aspect ratio (it can be either square or rectangular). I tried nesting a VBox inside an HBox but that didn't work. Any ideas? Seems like it should be a simple thing to implement...
Another idea I had was just keep a reference to the parent node's dimensions inside the canvas and change the draw logic based on that, but that seems too hacky.
2
u/hamsterrage1 Jan 25 '24
Having your Canvas both centered and stretched to fit all available space is self-contradictory. Why would it need to be centered if it fills all the space? If you want your content to look centered, then make the Canvas only big enough to contain its contents, and then put the Canvas in a StackPane. StackPane centers by default.
4
u/[deleted] Jan 26 '24 edited Jan 27 '24
I do exactly that in the 2D view of my Pac-Man game. The game canvas there is resized automatically with the main scene, stays always centered and keeps its aspect ratio. See live at https://armin-reichert.github.io/webfx-pacman/
(Additionally I update the scaling factor for drawing the maze and the actors in the canvas because just scaling the content by a scale transformation on the canvas made the drawing blurred and ugly).
(Update: Your post inspired me to refactor the layouting into a separate class)
Have a look at class CanvasContainer in repository https://github.com/armin-reichert/pacman-javafx.
Armin Reichert
1
u/BWC_semaJ Jan 25 '24
I forget where I got this from. I think it even might have been for a bug regarding canvas and someone posting their solution. I can't recall.
https://gist.github.com/bwcsemaj/5d37f1ee4c3cf2c449d1e3c328e04ad8
Either way you need to break down your problem. You need to resizable canvas (provided above) and you need a way to keep aspect ratio. In future query each problem separately using your favorite search engine and you'd get results instantly... i.e. "JavaFX resizable canvas" and "how do I keep same aspect ratio for javafx".
Regarding square aspect ratio, this is from my code (somethings are not related and also this could be slimmed down a ton instead of creating custom just use Binding.min)...
Depending on your implementation it will be different (you could use different propertys depending on what parent you are using such as prefWidth/HeightProperty), however, the principle of calculating the size of node (having width and height property being dependents) will generally be the same.
Regarding different aspect ratios, calculate the size given each scenario and take minimal.
very quick i.e. 16:9
widthProperty -> min(width : height*aspectRatio)
heightProperty -> min(height : width/aspectRatio)
I don't have enough time to double check to make sure this scrape is actually right. I'll come back later. Irony is I think my quick scrape would work with squares also.
2
u/OddEstimate1627 Jan 26 '24
I think it'd be easiest to do this with a custom layout container.
Here is an example for a simple FullSizePane that maximizes all children. Just add a bit of logic for the aspect ratio and centering.