Resizing JavaFX Windows while maintaining aspect ratio?

I have a window that I have to keep squared. My code

 primaryStage.minHeightProperty().bind(scene.widthProperty());
 primaryStage.minWidthProperty().bind(scene.heightProperty());

It changes the size of the square when expanding, but has problems when I try to make it smaller? Sometimes one side becomes a little shorter or longer than the other. Can this be fixed somehow? Am I doing something wrong in the code that I am currently using?

+4
source share
1 answer

, , UX, . , , , :

private val DEF_PAD = 6.0
chart.paddingProperty.bind(boundValue(stage.widthProperty, stage.heightProperty) {
    // Maintain a square plot with padding
    val w = stage.getWidth
    val h = stage.getHeight
    val extra = DEF_PAD + 0.5 * Math.abs(w - h)
    if (w > h) {
        new Insets(DEF_PAD, extra, DEF_PAD, extra)
    } else if (h > w) {
        new Insets(extra, DEF_PAD, extra, DEF_PAD)
    } else {
        new Insets(DEF_PAD)
    }
})

( Scala. boundValue - , varargs)

+1

All Articles