B4J Question Resize window but keeping aspect ratio

Blueforcer

Well-Known Member
Licensed User
Longtime User
Hey friends,

the base size of my window is 1024*600.
i want that the user is able to resize the window but with keeping the aspect ratio.
it should not be possible to only change the height or only the width
Is this possible?
 

stevel05

Expert
Licensed User
Longtime User
Something like this?

B4X:
#Region Project Attributes
    #MainFormWidth: 1024
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private WHRatio As Double
    Dim WidthChanged,HeightChanged As Boolean
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    WHRatio = MainForm.Width / MainForm.Height
 
    Dim Stage As JavaObject = MainForm.As(JavaObject).GetField("stage")
 
    Dim widthProperty As JavaObject = Stage.RunMethod("widthProperty",Null)
    Dim HeightProperty As JavaObject = Stage.RunMethod("heightProperty",Null)
     
    Dim Listener As Object = widthProperty.CreateEvent("javafx.beans.value.ChangeListener","WidthChanged","")
    widthProperty.RunMethod("addListener",Array(Listener))
 
    Dim Listener As Object = HeightProperty.CreateEvent("javafx.beans.value.ChangeListener","HeightChanged","")
    HeightProperty.RunMethod("addListener",Array(Listener))
End Sub

Private Sub WidthChanged_Event(MethodName As String,Args() As Object) As Object        'ignore
    If HeightChanged Then
        HeightChanged = False
        Return
    End If
    MainForm.WindowHeight = MainForm.WindowWidth / WHRatio
    WidthChanged = True
 
End Sub

Private Sub HeightChanged_Event(MethodName As String,Args() As Object) As Object        'ignore
    If WidthChanged Then
        WidthChanged = False
        Return
    End If
    MainForm.WindowWidth = MainForm.WindowHeight * WHRatio
    HeightChanged = True
End Sub
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Something like this?

B4X:
#Region Project Attributes
    #MainFormWidth: 1024
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private WHRatio As Double
    Dim WidthChanged,HeightChanged As Boolean
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    WHRatio = MainForm.Width / MainForm.Height
 
    Dim Stage As JavaObject = MainForm.As(JavaObject).GetField("stage")
 
    Dim widthProperty As JavaObject = Stage.RunMethod("widthProperty",Null)
    Dim HeightProperty As JavaObject = Stage.RunMethod("heightProperty",Null)
    
    Dim Listener As Object = widthProperty.CreateEvent("javafx.beans.value.ChangeListener","WidthChanged","")
    widthProperty.RunMethod("addListener",Array(Listener))
 
    Dim Listener As Object = HeightProperty.CreateEvent("javafx.beans.value.ChangeListener","HeightChanged","")
    HeightProperty.RunMethod("addListener",Array(Listener))
End Sub

Private Sub WidthChanged_Event(MethodName As String,Args() As Object) As Object        'ignore
    If HeightChanged Then
        HeightChanged = False
        Return
    End If
    MainForm.WindowHeight = MainForm.WindowWidth / WHRatio
    WidthChanged = True
 
End Sub

Private Sub HeightChanged_Event(MethodName As String,Args() As Object) As Object        'ignore
    If WidthChanged Then
        WidthChanged = False
        Return
    End If
    MainForm.WindowWidth = MainForm.WindowHeight * WHRatio
    HeightChanged = True
End Sub
Thank you very much,
its a bit buggy but i think a can figure it out
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yeah, it's not going to be perfectly smooth as the values are dependant on each other, you might want to try binding the values. There are examples on stackoverflow, which seem to have other drawbacks.
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
my main problem right now is that if you increase the window size only one pixe the width gets around 80 pixels wider and height a bit smaller. the aspect ratio doesnt seems to fit anymore. But i dont see the error at the moment
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK Try:
B4X:
    WHRatio = MainForm.WindowWidth / MainForm.WindowHeight

At line 18 in the original code,
 
Upvote 0
Top