B4J Question Draw Line with Gradient

postasat

Active Member
Licensed User
Longtime User
Hi,
I need to draw a line with colour gradient between 2 colours.
Is it possible in B4J ?

I'm using this code but is not possible to use two colours:
B4X:
cvsScreen1.DrawLine(0,0,x1,y1,colour1, 10dip)

Thank you.
 

klaus

Expert
Licensed User
Longtime User
You can do it, needs the JavaObject library:
B4X:
    Private x0, y0, x1, y1 As Int
    x0 = 50
    y0 = 450
    x1 = 200
    y1 = 450
    g = "linear-gradient(from " & x0 & "px " & y0 & "px to " & x1 & "px " & y1 & "px, #ff0000 0%, 0x0000ff 100%)"
    cvsScreen1.DrawLine(x0, y0, x1, y1, GetPaint(g), 10)
 
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

Attached a small test project showing the possibilities.

Documentation:
https://docs.oracle.com/javase/8/ja...LinearGradient.html#valueOf-java.lang.String-

You may also have a look at the jCanvasExt Class.
 

Attachments

  • TestCanvas.zip
    70.4 KB · Views: 239
Upvote 0
Top