B4J Question Problem with Designer & TextField

SeaBee

Member
Licensed User
First this:

Waiting for debugger to connect...
Program started.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by anywheresoftware.b4a.shell.Shell (file:/C:/Program%20Files%20(x86)/Anywhere%20Software/B4J/Libraries/jDebug.jar) to field javafx.scene.control.TextField.prefColumnCount
WARNING: Please consider reporting this to the maintainers of anywheresoftware.b4a.shell.Shell
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release


I just wanted to develop an equivalent to the old HP BASIC ANGLE(X,Y) function using B4J. This function returned ArcTan2 in the correct quadrant such that -π < θ <= π. I used the designer to build a very simple form - two input TextField views for X and Y, two Buttons, and two output TextField views to show in one ATan2D and the other one to show ANGLE from my code, in order to compare results.

First problem was a crash because I had not initialized the TextField views, which I did not think was necessary as I had generated the required entries into the Main module. Once I had manually initialized them the program ran, but was not reading the text from the TextField views.

It couldn't be much simpler than this (although it could be a lot less verbose) so I suspect I have made an idiot blunder somewhere - this is the first time I have used B4J (V. 7.51).

B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Private txtXUnits As TextField
 Private txtYUnits As TextField
 Private Label1 As Label
 Private Label2 As Label
 Private Button1 As Button
 Private Button2 As Button
 Private txtATan2D As TextField
 Private txtAngle As TextField
 
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
 MainForm.RootPane.LoadLayout("AngleTest") 'Load the layout file
 MainForm.Show
 
 txtXUnits.Initialize("txtXUnits")
 txtYUnits.Initialize("txtYUnits")
 
End Sub

Sub Button2_Click
 
End Sub

Sub Button1_Click

 Dim xUnits As String
 Dim yUnits As String
 
 xUnits = txtXUnits.Text
 yUnits = txtYUnits.Text
 
 Dim dblX As Double
 Dim dblY As Double
 
 If IsNumber(xUnits) Then
  dblX = xUnits
 Else
  dblX = 0
 End If
 
 If IsNumber(yUnits) Then
  dblY = yUnits
 Else
  dblY = 0
 End If

 Dim Tangent2 As  Double = ATan2D(dblY, dblX)
 Dim strTan2 As String = Tangent2
 
 txtATan2D.Text = strTan2
 txtAngle.Text = strTan2 'This last line to put a break-point on to inspect variables
 
End Sub
 

SeaBee

Member
Licensed User
1. Thanks - I only saw it after the first time after I manually initialized the TextField views.

2. The view names in the listing were generated by the designer:-

B4X:
Private txtXUnits As TextField
Private txtYUnits As TextField

Anyway, I zapped the two offending items, and rebuilt the views with a different name in the designer:-

B4X:
Private txtXvalue As TextField
Private txtYvalue As TextField

Now all is well, but I am still mystified about the problem, as the only attribute/property I changed in the designer was the view name.
 
Upvote 0

SeaBee

Member
Licensed User
Thinking back, I it may be that I ran Generate, and then manually changed the names on the layout and in Main, but no matter - it all works now - thanks!
 
Upvote 0

emexes

Expert
Licensed User
develop an equivalent to the old HP BASIC ANGLE(X,Y) function using B4J. This function returned ArcTan2 in the correct quadrant such that -π < θ <= π.
I used this method before I discovered B4A had ArcTan2 :-/ when converting accelerometer readings to phone orientation angle. I have a feeling that the X reading is the "wrong" way around (presumably to be consistent between portrait and landscape orientations) so the "X < 0" might need changing to "X > 0" (or something like that).
B4X:
X = acceleration on x axis
Y = acceleration on y axis
      
Dim R As Float = Sqrt(X * X + Y * Y )
                                        
If R > 1 Then    'arbitrary cut-off of orientation detection as phone is tilted towards flat horizontal
  
    Dim ArcCosY As Float = ACos(Y / R) / cPI * 180
      
    If X < 0 Then
        NewDeviceAngle = ArcCosY    'returns angles 0 .. 180
    Else
        'choose one of the below, depending on whether need angles -180 .. 180 or 0 .. 360
        '''NewDeviceAngle = -ArcCosY    'returns angles -180 .. 0
        NewDeviceAngle = 360 - ArcCosY    'returns angles 180 .. 359
    End If
      
End If
 
Upvote 0

SeaBee

Member
Licensed User
I wanted to verify that ATan2 and ATan2D both handled X = 0 or Y = 0 elegantly, which ANGLE(X,Y) did (they do). Stage two was building a function that returned the bearing from either of the ATan2 functions on a plane surface.
 
Upvote 0

emexes

Expert
Licensed User
a function that returned the bearing from either of the ATan2 functions on a plane surface.
I don't completely understand the plane surface bit, but that says more about my maths skills than your phrasing.

On a half-related note: I used ArcCos because I thought it was quicker than ArcTan since Tan = Sin / Cos (or something like that) but... I might have made a bad assumption there :-/

If your calculations on a plane surface are stepping towards being for the earth's surface, then this guy is the master:

https://edwilliams.org/avform.htm

Twice I have thought he'd made a mistake, and twice I have been wrong :)

(ie, he is the Erel of Navigation!)
 
Upvote 0

SeaBee

Member
Licensed User
I don't completely understand the plane surface bit, but that says more about my maths skills than your phrasing.

If your calculations on a plane surface are stepping towards being for the earth's surface, then this guy is the master:

https://edwilliams.org/avform.htm
The plane surface means it is good for things like UK Ordnance Survey maps and Admiralty charts because of the projection they use, but may not be used where the Earth's curvature has to be considered. The functions can also be easily adapted to three dimensions, so I guess that makes them good for space travel. :D

I have been using Ed William's Aviation Formulary for 15 years now, and his heading formula is excellent for deriving a rhumb line course between distant points, or the course components of a great circle course.
 
Upvote 0
Top