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?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User



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.



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: 456
  • Mandelbrot_B4i.zip
    66.8 KB · Views: 497
  • Mandelbrot_B4J.zip
    8 KB · Views: 459
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…