B4J Code Snippet Draw text with outline

Use javafx canvas to draw text with outline. Better to use CanvasExt lib from @klaus but the working principle is the same. To make fancy text you can also use Graphiclib by @stevel05. Some libs released several years ago are gems.

B4X:
    Dim jcv As JavaObject = Cv 'canvas
    Dim jgd As JavaObject = jcv.RunMethod("getGraphicsContext2D", Null)
    Dim jco As JavaObject
    Cv.ClearRect(0,0,Cv.Width, Cv.Height)
    txx = Cv.Width * 0.5
    tyy = Cv.Height * 0.5
    jco.InitializeNewInstance("javafx.scene.paint.Color", Array As Object(1.0, 0.0, 0.0, 1.0))
    Dim f As Font = fx.CreateFont("Arial", 80, False, False)
    jgd.RunMethod("setFont", Array As Object(f))
    jgd.RunMethod("setStroke", Array As Object(jco))
    jgd.RunMethod("setLineWidth", Array As Object(4.0))
    jgd.RunMethod("strokeText", Array As Object("kimstudio", txx, tyy))
    jco.InitializeNewInstance("javafx.scene.paint.Color", Array As Object(0.0, 0.0, 1.0, 1.0))
    jgd.RunMethod("setFill", Array As Object(jco))
    jgd.RunMethod("fillText", Array As Object("kimstudio", txx, tyy))

fo.png
 

kimstudio

Active Member
Licensed User
Longtime User
Hi xulihang, I didn't finish the fonteffector as many image editing softwares can do that so I stopped it very soon and internally it just uses the code I share here. The Cv is the canvas put by designer. Regarding shadow effect I did't realize it, but can be implemented by using javafx.effect via javaobject.
 
Top