#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private startref As Long
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
        
    '**************** draw a rounded gradient on a B4XCanvas (with path)
    Dim cvs As B4XCanvas
    cvs.Initialize(MainForm.RootPane)
    Dim r As B4XRect
    r.Initialize(60,60,90,150)
    startref = DateTime.now
    For i = 0 To 1000
        DrawRoundRectGradient(cvs, r, 30, Array As Int(xui.Color_Red,xui.Color_Blue))
    Next
    Log("B4XCanvas Time: "&(DateTime.Now-startref))       
    
    '**************** draw a rounded gradient on a B4XCanvas (with brush)
    
    r.Initialize(120,60,150,150)
    startref = DateTime.now
    For i = 0 To 1000
        DrawRoundRectGradient2(cvs, r, 30, Array As Int(xui.Color_Red,xui.Color_Blue))
    Next
    Log("B4XCanvas(2) Time: "&(DateTime.Now-startref))   
    
    '**************** draw a rounded gradient on a native Canvas
    
    Dim g As String = "linear-gradient(from 0% 0% to 100% 100%, #ff0000 0%, 0x0000ff 100%)"
    Dim rp As B4XView = MainForm.RootPane
    Dim cv As Canvas = rp.GetView(0)
    startref = DateTime.now
    For i=0 To 1000
        DrawRectRounded(cv,180,60,30,90,30,30,GetPaint(g),True,0)
    Next
    Log("Canvas Time: "&(DateTime.Now-startref))
    
    cvs.Invalidate
    cvs.Release
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Private Sub DrawRoundRectGradient(cvs As B4XCanvas, rct As B4XRect, Radius As Float, Colors() As Int)
    Private bc As BitmapCreator
    Private rct1 As B4XRect
    
    rct1.Initialize(0, 0, rct.Width, rct.Height)
    bc.Initialize(rct.Width, rct.Height)
    bc.FillGradient(Colors, rct1, "TOP_BOTTOM")
    
    Private Path As B4XPath
    Path.InitializeRoundedRect(rct, Radius)
    
    cvs.ClipPath(Path)
    cvs.DrawBitmap(bc.Bitmap, rct)
    cvs.RemoveClip
End Sub
Private Sub DrawRoundRectGradient2(cvs As B4XCanvas, rct As B4XRect, Radius As Float, Colors() As Int)
    Private gradient,bc As BitmapCreator
    Private rct1 As B4XRect
    
    rct1.Initialize(0, 0, rct.Width, rct.Height)   
    gradient.Initialize(rct.Width, rct.Height)
    gradient.FillGradient(Colors, rct1, "TOP_BOTTOM")
        
    Dim gradientBrush As BCBrush = gradient.CreateBrushFromBitmapCreator(gradient)
    bc.Initialize(rct.Width, rct.Height)
    bc.DrawRectRounded2(bc.TargetRect, gradientBrush, True, 0, Radius )
    cvs.DrawBitmap(bc.Bitmap, rct)
End Sub
'Returns the paint object according to the color string
Public Sub GetPaint(Color As String) As Paint
    Private joPaint As JavaObject
    joPaint = joPaint.InitializeStatic("javafx.scene.paint.Paint")
    Return joPaint.RunMethod("valueOf", Array As Object(Color))
End Sub
'Draws a rouded Rectangle
'x = left coordinate of the surrounding rectangle
'Y = top coordinate of the surrounding rectangle
'Width = width coordinate of the surrounding rectangle
'Height = height coordinate of the surrounding rectangle
'ArcWidth = corner arc width
'ArcHeight = corner arc height
'Color = fx.Colors.Color
'Filled  True Filled, False only the line
'Stroke = line width, has no effect when Filled = True
Public Sub DrawRectRounded(MyCanvas As Canvas, x As Double, y As Double, Width As Double, Height As Double, ArcWidth As Double, ArcHeight As Double, Color As Paint, Filled As Boolean, LineWidth As Double)
    Private joCanvas, joGraph As JavaObject
    joCanvas = MyCanvas
    joGraph = joCanvas.RunMethod("getGraphicsContext2D", Null)
    If Filled = False Then
        joGraph.RunMethod("setStroke", Array As Object(Color))
        joGraph.RunMethod("setLineWidth", Array As Object(LineWidth))
        joGraph.RunMethod("strokeRoundRect", Array As Object(x, y, Width, Height, ArcWidth, ArcHeight))
    Else
        joGraph.RunMethod("setFill", Array As Object(Color))
        joGraph.RunMethod("fillRoundRect", Array As Object(x, y, Width, Height, ArcWidth, ArcHeight))
    End If
End Sub