Android Question Canvas delete my draws

hibrid0

Active Member
Licensed User
Longtime User
Hi learning how to use canvas, I test an small app with B4J and work.
Now I'm making it on B4A and not work fine and I dont know why.
First when I start to paint and touch the panel, the panel change color from white to black and the first trace not showed, the next draws show, but when touch again is deleted.

I use 2 canvas, 1 for temp draws and second to persistent draws, on b4j work fine, but on b4a not.
If I set the panel color to white, cant paint anything or I dont see any trace.
Anybody will showme whats is my error?

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Panel1 As B4XView
    Private xui As XUI
    Private font1 As B4XFont
    Private canvas1 As B4XCanvas
    Private canvas_temp As B4XCanvas
    Dim posX_inicio, posY_inicio As Double
    Dim imagen1 As B4XBitmap
    Dim result As String
    Dim Rectangulo As B4XRect
    Dim flecha As B4XPath
    Dim dibujo1 As B4XBitmap
 
    Private btn_borrar As Button
    Private btn_circulo As Button
    Private btn_cuadrado As Button
    Private btn_linea As Button
    Private btn_texto As Button
 
'    Private ColorPicker1 As ColorPicker
    Dim Dibujar As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("frm_esquema")
    Activity.Title=functions.titulo
    canvas1.Initialize(Panel1)
    canvas_temp.Initialize(Panel1)
    font1 = xui.CreateDefaultFont(14)
    Dibujar="linea"
    'Panel1.Color= xui.Color_White
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
 
    Log("Action: "&Action)
    Rectangulo.Initialize(posX_inicio,posY_inicio, x, y)
 
    If Action=0 Then
        posX_inicio=X
        posY_inicio=Y
 
'        dibujar_linea(X, Y, False)
 
    else if Action=2 Then
        canvas_temp.ClearRect(canvas1.TargetRect)
'        canvas_temp.DrawLine(x, y,posX_inicio , posY_inicio, xui.Color_Black, 2)
'        canvas_temp.DrawCircle(posX_inicio, posY_inicio, 1*x,xui.Color_Black, False, 2)

     
        Select Dibujar
            Case "linea" 'Line
                dibujar_linea(X, Y, False)
            Case "cuadrado" 'Rectangle
                dibujar_rectangulo(X, Y, False)
            Case "circulo"
                dibujar_circulo(X, Y, False)
            Case "borrar" 'delete
                borrar(X, Y, False)
            Case "texto"
                Log("Larger than 9")
        End Select
    Else if Action=1 Then
        Select Dibujar
            Case "linea"
                dibujar_linea(X, Y, True)
            Case "cuadrado"
                dibujar_rectangulo(X, Y, True)
            Case "circulo"
                dibujar_circulo(X, Y, True)
            Case "borrar"
                borrar(X, Y, True)
            Case "texto"
                Log("Larger than 9")
        End Select
        canvas1.Invalidate
    else if Action=100 Then
 
     
     
    End If
 
End Sub


Sub btn_texto_Click
    Dibujar="texto"
 
 
 
End Sub

Sub btn_linea_Click
    Dibujar="linea"
End Sub

Sub btn_cuadrado_Click
    Dibujar="cuadrado"
End Sub

Sub btn_circulo_Click
    Dibujar="circulo"
End Sub

Sub Flecha1 (x, y)
    flecha.Initialize(0, -10)
    flecha.lineTo(5, 0)
    flecha.lineTo(-5, 0)
End Sub

Sub dibujar_rectangulo (x As Double, y As Double, final As Boolean)
    Rectangulo.Top=posY_inicio
    Rectangulo.Left=posX_inicio
    Rectangulo.Bottom = y
    Rectangulo.Right= x
 
 
    If posY_inicio < y  Then
        Rectangulo.Top=posY_inicio
        Rectangulo.Left=posX_inicio
        Rectangulo.Bottom=y
        Rectangulo.Right=x
    else if posY_inicio > y Then
        Rectangulo.Top=y
        Rectangulo.Left=posX_inicio
        Rectangulo.Bottom=posY_inicio
        Rectangulo.Right=x
    else if posX_inicio < x Then
'            Rectangulo.Top = posY_inicio
'            Rectangulo.Left = x
'            Rectangulo.Bottom = y
'            Rectangulo.Right = posX_inicio
    Else if posX_inicio > x Then
        Rectangulo.Top = y
        Rectangulo.Left = x
        Rectangulo.Bottom = posY_inicio
        Rectangulo.Right = posX_inicio
    End If
    If final = False Then
        canvas_temp.DrawRect(Rectangulo, xui.Color_red, False, 2)
    Else
        canvas1.DrawRect(Rectangulo, xui.Color_Black, False, 2)
        canvas1.Invalidate
    End If
 
End Sub

Sub borrar(x As Double,y As Double, final As Boolean)
 
    Dim Rectangulo1 As B4XRect
    Rectangulo1.Top = y
    Rectangulo1.Left = x
    Rectangulo1.Bottom = y+5dip
    Rectangulo1.Right = x+5dip
    If final = False Then
        canvas_temp.DrawRect(Rectangulo, xui.Color_White, True, 2)
    Else
        canvas1.DrawRect(Rectangulo, xui.Color_white, True, 2)
        canvas1.Invalidate
    End If
End Sub

Sub dibujar_circulo(x As Double, y As Double, final As Boolean)
    Rectangulo.Top=posY_inicio
    Rectangulo.Left=posX_inicio
    Rectangulo.Bottom = y
    Rectangulo.Right= x
    If final = False Then
'        canvas_temp.DrawCircle(X, Y, 10+posX_inicio, xui.Color_Black, False, 2)
        canvas_temp.DrawCircle(x, y, Rectangulo.Width, xui.Color_red, False, 2)
    Else
        canvas1.DrawCircle(X, Y, Rectangulo.Width, xui.Color_Black, False, 2)
        canvas1.Invalidate
    End If
 
End Sub

Sub dibujar_linea(x As Double, y As Double, final As Boolean)
    Log("X y Y")
    Log("x: "&x &"y: "&y)
    Log("x y y iniciales")
    Log(posX_inicio &":"&posY_inicio)
    If final= False Then
        canvas_temp.DrawLine(X, Y,posX_inicio , posY_inicio, xui.Color_Red, 2)
    Else
        canvas1.DrawLine(X, Y,posX_inicio , posY_inicio, xui.Color_Red, 2)
        canvas1.Invalidate
    End If
 
End Sub


Sub btn_borrar_Click
    Dibujar="borrar"
End Sub
 

Attachments

  • pintar1.zip
    103.6 KB · Views: 282
Last edited:

klaus

Expert
Licensed User
Longtime User
It would be easier for us to help you if you posted your project as a zip file!

upload_2019-1-4_12-9-1.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
You need two Panels one for each Canvas!
You cannot use two canvases on the same Panel!
Setting the color to transparent in the Designer, you need to set Alpha to 0.
You may have a look at anchors for the layout.

You say that your code works with B4J.
I think that you used tow B4J Canvases but not B4XCanvases.
Otherwise, if the code is similar to the B4A code it will not work.
 

Attachments

  • pintarNew.zip
    103.4 KB · Views: 278
Upvote 0
Top