Hi all,
thank you for your hints. I still have to check Roycefer's lib.
In the meanwhile I try to explain better what is my need: to draw a line with exact given "dimensions", so a bar whose height is supposed to be 3.47mm and thick 0.33mm appears so on any device. The use of the code in post #1 let me do that on Android devices, but when it comes to B4J and java it seems there's no equivalent.
Please find below the getDisplayMetrics along with values from my Huawey Ascend Y550 phone
'returns Display metrics in inches or millimeters
Private Sub getDisplayMetrics(Xaxis As Boolean, UnitInches As Boolean) As Float
Dim xdpi, ydpi As Float
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
r.Target = r.RunMethod("getDisplayMetrics")
xdpi=r.GetField("xdpi")
ydpi=r.GetField("ydpi")
Log("------Android data -----")
Log(r.GetField("densityDpi")) '240 dpi
Log(r.GetField("widthPixels")) '480 px
Log(r.GetField("heightPixels")) '854 px
Log(r.GetField("xdpi")) '221.672 The exact physical pixels per inch of the screen in the X dimension.
Log(r.GetField("ydpi")) '219.107 The exact physical pixels per inch of the screen in the Y dimension.
Note that
widthPixels and
heightPixels switch hwen rotating the phone, while other values stay the same.
When it comes to B4J and java I tried the following (my Aspire 5740G notebook display is set to 1366x768px) :
Private Sub DisplayMetrics2
Log("------ java.awt.Toolkit data -------")
Dim jo As JavaObject
jo.InitializeStatic("java.awt.Toolkit")
Dim toolkit As JavaObject = jo.RunMethod("getDefaultToolkit", Null)
Dim ScreenRes As Int = toolkit.RunMethod("getScreenResolution", Null)
Log("screenRes: "&ScreenRes) '--> 96dpi
Dim Screensize As JavaObject = toolkit.RunMethod("getScreenSize", Null)
Dim jo2 As JavaObject = Screensize
Log("width: "&jo2.RunMethod("getWidth",Null)) '1366px
Log("height: "&jo2.RunMethod("getHeight",Null)) '768px
Log("------ javafx.stage.Screen data -------")
Dim screen As JavaObject
Log(screen.InitializeStatic("javafx.stage.Screen").RunMethodJO("getPrimary",Null).RunMethod("getDpi",Null))
End Sub
Comparing these values with those from Android, it's like reading
widthPixels,
heightPixels and
densityDpi (an approximate value). But nothing near xdpi and ydpi.
So, how am I supposed to draw a line with exact given stroke width and height?
Edit: I did a quick test making use of ScreenRes and it apparently worked. Tomorrow I hope to have some spare time to write a better test to upload here.
udg