Android Question [B4X] Mandelbrot Fractal to B4A

bparent

Member
Licensed User
Longtime User
This seems like a good way to learn how to translate or use B4J to B4A. How would one go about translating the B4J code to B4X and using the code with B4A/B4X?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2018-04-23_08.29.45.png

SS-2018-04-23_08.30.27.png


Steps to port the B4J example to B4A and B4i:

0. Update the B4J code to use the latest version of BitmapCreator.
1. Move the relevant code to a class.
2. Share the modules between the projects:

Make sure to select Link and not copy.

share.gif


3. Create the same layout in B4A and B4i.

4. In B4J and B4i the scale (dots per inch) is always 1. In B4A the scale changes. When working with BitmapCreator we need to divide the dimensions by the scale.
5. The panel events are the same in B4A and B4i. They are a bit different in B4J. This is taken care with:
B4X:
    #if Not (B4J)
   Type MouseEvent (X As Int, Y As Int)
   Private DummyPanel As Panel 'ignore
   #End If

#if Not(B4J)
Sub Pane1_Touch (Action As Int, X As Float, Y As Float)
   Dim ev As MouseEvent
   ev.X = X
   ev.Y = Y
   If Action = DummyPanel.ACTION_DOWN Then
       Pane1_MousePressed(ev)
   Else if Action = DummyPanel.ACTION_MOVE Then
       Pane1_MouseDragged(ev)
   Else
       Pane1_MouseReleased(ev)
   End If
End Sub
#End If

6. In B4J and B4i the page or form can be resized at runtime. In most cases you will need to add a resize sub that handles this. As the original example didn't handle resizing I preferred not to add it here. The code in Main in B4i waits for the first page resize event and only then initializes the class.

The three projects are attached. BitmapCreator and Mandelbrot classes are identical and should be linked between the three projects.
 

Attachments

  • Mandelbrot_B4A.zip
    15.1 KB · Views: 349
  • Mandelbrot_B4i.zip
    66.8 KB · Views: 364
  • Mandelbrot_B4J.zip
    8 KB · Views: 343
Upvote 0
Top