Android Question How can I call Paint.setStrokeCap?

NeoTechni

Well-Known Member
Licensed User
Longtime User
https://developer.android.com/refer...html#setStrokeCap(android.graphics.Paint.Cap)

This page shows it's a valid method for the paint object, so I tried

B4X:
'CapType: 0=Butt, 1=Round, 2=Square (assumed)
Sub setStrokeCap(BG As Canvas, CapType As Int)
   Dim r As Reflector
  r.Target = BG
  r.Target = r.GetField("paint")
  r.RunMethod2("setStrokeCap", CapType, "java.lang.int")
End Sub

but I got:

graphics_setstrokecap (java line: 3915)
java.lang.NoSuchMethodException: setStrokeCap [int]
at java.lang.Class.getMethod(Class.java:624)
at java.lang.Class.getDeclaredMethod(Class.java:586)
at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:817)
at com.omnicorp.scifiui.graphics._setstrokecap(graphics.java:3915)
at com.omnicorp.scifiui.omnipaint._drawlinelist(omnipaint.java:1962)
at com.omnicorp.scifiui.omnipaint._drawpoints(omnipaint.java:2232)
at com.omnicorp.scifiui.omnipaint._addpoint(omnipaint.java:391)
at com.omnicorp.scifiui.omnipaint._drawlayer_down(omnipaint.java:1746)
at com.omnicorp.scifiui.omnipaint._handleevent(omnipaint.java:5128)
at com.omnicorp.scifiui.macintosh._handleevent(macintosh.java:2842)
at com.omnicorp.scifiui.macintosh._handletouch(macintosh.java:3526)
at com.omnicorp.scifiui.lcar._mouseevent(lcar.java:8738)
at com.omnicorp.scifiui.lcar._gesturestouch(lcar.java:4840)
at com.omnicorp.scifiui.main._handledelayedtouch(main.java:2225)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
java.lang.NoSuchMethodException: setStrokeCap [int]

Also, I'm assuming the values of the enum. Is there a way to get the real values?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is there a way to get the real values?
enums do not have any "other" value.

B4X:
Dim paint As JavaObject = BG
paint = BG.GetField("paint") 'this line will fail in older versions of B4A. If it happens then you should use Reflector to get the paint and then set it to a JavaObject variable.
paint.RunMethod("setStrokeCap", Array("ROUND"))
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
'CapType: 0=Butt, 1=Round, 2=Square
Sub setStrokeCap(BG As Canvas, CapType As Int)
   Dim paint As JavaObject = BG, CapName As String
   Select Case CapType
     Case 0: CapName = "BUTT"
     Case 1: CapName = "ROUND"
     Case 2: CapName = "SQUARE"
   End Select
   paint = paint.GetField("paint") 'this line will fail in older versions of B4A. If it happens then you should use Reflector to get the paint and then set it to a JavaObject variable.
   paint.RunMethod("setStrokeCap", Array(CapName))
End Sub
What I used.

Thank you.
 
Upvote 0
Top