Android Question Booklet: XUISourceCode > SimpleDrawMethods > B4A

Diceman

Active Member
Licensed User
I downloaded the Booklets and compiled Booklets > XUI > XUISourceCode > SimpleDrawMethods

When I run it in B4A 9.90 I get an error right away in main_drawlayer2 sub.

B4X:
Private Sub DrawLayer2
    'draw background color
    Log("draw background color")
    cvsLayer(2).DrawRect(xbckRect, xui.Color_ARGB(255, 240, 240, 240), True, 1dip)
    Log("draw the four lines on layer(2)")
    ' draw the four lines on layer(2)
    x1 = 10dip
    y1 = 10dip
    x2 = 150dip
    y2 = 40dip
    cvsLayer(2).DrawLine(x1, y1, x2, y2, xui.Color_Red, 1dip)
    y1 = 50dip
    y2 = 50dip
    cvsLayer(2).DrawLine(x1, y1, x2, y2, xui.Color_Green, 3dip)
    y1 = 60dip
    y2 = 90dip
    cvsLayer(2).DrawLine(x1, y1, x2, y2, xui.Color_Yellow, 5dip)
    y1 = 80dip
    y2 = 110dip
    cvsLayer(2).DrawLine(x1, y1, x2, y2, xui.Color_RGB(0, 128, 255), 8dip)
    Log("draw the three horizontal texts")
    ' draw the three horizontal texts
    x1 = 220dip
    y1 = 40dip
    cvsLayer(2).DrawText("Rose", x1, y1, xFont, xui.Color_Blue, "LEFT")
    DrawCross(x1, y1, xui.Color_Red)
    y1 = 70dip
    cvsLayer(2).DrawText("Rose", x1, y1, xFont, xui.Color_Blue, "CENTER")
    DrawCross(x1, y1, xui.Color_Red)
    y1 = 100dip
    cvsLayer(2).DrawText("Rose", x1, y1, xFont, xui.Color_Blue, "RIGHT")
    DrawCross(x1, y1, xui.Color_Red)
    Log("draw the three rotated texts")
    ' draw the three rotated texts
    x1 = 300dip
    y1 = 40dip
    cvsLayer(2).DrawTextRotated("Rose", x1, y1, xFont, xui.Color_Blue, "LEFT", -30)
    DrawCross(x1, y1, xui.Color_Red)
    y1 = 70dip
    cvsLayer(2).DrawTextRotated("Rose", x1, y1, xFont, xui.Color_Blue, "CENTER", -30)
    DrawCross(x1, y1, xui.Color_Red)
    y1 = 100dip
    cvsLayer(2).DrawTextRotated("Rose", x1, y1, xFont, xui.Color_Blue, "RIGHT", -30)
    DrawCross(x1, y1, xui.Color_Red)
    Log("draw an empty circle")
    'draw an empty circle
    xc = 100dip
    yc = 180dip
    Radius = 50dip
    cvsLayer(2).DrawCircle(xc, yc, Radius, xui.Color_Red, False, 5dip)
    Log("draw a clipped arc")
    'draw a clipped arc
    Private mPath As B4XPath
    xc = 10dip
    yc = 320dip
    Radius = 80dip
    mPath.InitializeArc(xc, yc, Radius, -30, 60)
    cvsLayer(2).ClipPath(mPath)
    cvsLayer(2).DrawRect(cvsLayer(2).TargetRect, xui.Color_Magenta, True, 1dip)
    cvsLayer(2).RemoveClip
    Log("draw a clipped triangle")
    'draw a clipped triangle
    mPath.Initialize(100dip, 280dip)
    mPath.LineTo(180dip, 280dip)
    mPath.LineTo(140dip, 360dip)
    cvsLayer(2).ClipPath(mPath)
    cvsLayer(2).DrawRect(cvsLayer(2).TargetRect, xui.Color_Blue, True, 1dip)
    cvsLayer(2).RemoveClip
    Log("draw an arc Path")
    'draw an arc Path
    Private mPath As B4XPath
    xc = 10dip
    yc = 420dip
    Radius = 80dip
    mPath.InitializeArc(xc, yc, Radius, -30, 60)
    cvsLayer(2).DrawPath(mPath, xui.Color_Green, True, 1dip)
    cvsLayer(2).RemoveClip
    Log("draw a triangle path")
    'draw a triangle path
    Private mPath As B4XPath
    mPath.Initialize(90dip, 380dip)
    mPath.LineTo(170dip, 380dip)
    mPath.LineTo(130dip, 460dip)
    cvsLayer(2).DrawPath(mPath, xui.Color_Green, True, 1dip)
    cvsLayer(2).RemoveClip
    Log("draw a rotated triangle path with the center")
    'draw a rotated triangle path with the center
    Private mPath As B4XPath
    mPath.Initialize(180dip, 380dip)
    mPath.LineTo(260dip, 380dip)
    mPath.LineTo(220dip, 460dip)
    cvsLayer(2).DrawPathRotated(mPath, xui.Color_Green, True, 1dip, 15, 180dip, 380dip)
    cvsLayer(2).RemoveClip
    DrawCross(180dip, 380dip, xui.Color_Red)
    Log("draw a rotated triangle path with the center")
    'draw a rotated triangle path with the center
    Private mPath As B4XPath
    mPath.Initialize(260dip, 380dip)
    mPath.LineTo(340dip, 380dip)
    mPath.LineTo(300dip, 460dip)
    cvsLayer(2).DrawPathRotated(mPath, xui.Color_Green, True, 1dip, 15, 300dip, 405dip)
    cvsLayer(2).RemoveClip
    DrawCross(300dip, 405dip, xui.Color_Red)

    cvsLayer(2).Invalidate
    Log("saves the bitmap of layer(2)")
    'saves the bitmap of layer(2)
    Dim Out As OutputStream
    xui.SetDataFolder("SimpleDrawMethods")
    Out = File.OpenOutput(xui.DefaultFolder, "Test.png", False)
    cvsLayer(2).CreateBitmap.WriteToStream(Out, 100, "PNG")
    Out.Close
    
    cvsLayer(2).Release
End Sub

Here is the log with a description as it starts to execute each block of code. It failed in "draw and arc path".

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
draw background color
draw the four lines on layer(2)
draw the three horizontal texts
draw the three rotated texts
draw an empty circle
draw a clipped arc
draw a clipped triangle
draw an arc Path
main_drawlayer2 (java line: 678)
java.lang.IllegalStateException: Underflow in restore - more restores than saves
at android.graphics.Canvas.restore(Canvas.java:679)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.RemoveClip(CanvasWrapper.java:485)
at anywheresoftware.b4a.objects.B4XCanvas.RemoveClip(B4XCanvas.java:95)
at B4A.xSimpleDrawFunctions.main._drawlayer2(main.java:678)
at B4A.xSimpleDrawFunctions.main._drawing(main.java:459)
at B4A.xSimpleDrawFunctions.main._activity_resume(main.java:428)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at B4A.xSimpleDrawFunctions.main.afterFirstLayout(main.java:111)
at B4A.xSimpleDrawFunctions.main.access$000(main.java:17)
at B4A.xSimpleDrawFunctions.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
java.lang.IllegalStateException: Underflow in restore - more restores than saves

No idea why it's doing this. I also compiled the B4J version (B4J v8.31) and it displays a rose image in the background (see attached image) and nothing in the foreground except for 3 buttons at the bottom. Clicking on the buttons does not do anything apart from changing the color and text of the buttons. I would have thought clicking on the images would display different layers of images.

TIA
 

Attachments

  • Booklets_XUI_xjSimpleDrawMethods.png
    Booklets_XUI_xjSimpleDrawMethods.png
    188.2 KB · Views: 212

klaus

Expert
Licensed User
Longtime User
Your screenshot looks strange!
The program works on my computer.
You should see this when all buttons are green.

1595052543351.png


The program is updated for the next edition.
The problem was that a call To RemoveClip without having set a ClipPath before it, throughs an error in B4A but not in B4J.
 
Last edited:
Upvote 0
Top