Noobie Question about Drawing with Canvas

ebsebs

New Member
Licensed User
Longtime User
I have this routine that draws a traffic signal (red, green and yellow circles).
The "Lights" parameter is a 3 character string, consisting of the letters "R", "Y", and "G", in that order.
If the letter is uppercase, the corresponding light is ON, a lowercase letter means the light is OFF:

B4X:
Sub DrawLights(Lights As String)
   If Lights.SubString2(0, 1) = "R" Then
      Canvas1.DrawCircle(40dip, 200dip, 30dip, Colors.Red, True, 10dip)
   Else
      Canvas1.DrawCircle(40dip, 200dip, 30dip, Colors.Black, True, 10dip)
   End If
   If Lights.SubString2(1, 2) = "Y" Then
      Canvas1.DrawCircle(40dip, 280dip, 30dip, Colors.Yellow, True, 10dip)
   Else
      Canvas1.DrawCircle(40dip, 280dip, 30dip, Colors.Black, True, 10dip)
   End If
   If Lights.SubString2(2, 3) = "G" Then
      Canvas1.DrawCircle(40dip, 360dip, 30dip, Colors.Green, True, 10dip)
   Else
      Canvas1.DrawCircle(40dip, 360dip, 30dip, Colors.Black, True, 10dip)
   End If
   
   Activity.Invalidate
End Sub

I define the Canvas1 object in "Sub Globals" and initialize it like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
.
.
.
   Canvas1.Initialize(Activity)
End Sub

The first time I call the subroutine, the lights are drawn as expected. Every time after that, however, all three lights are always black.

Can someone please point out what (dumb) mistake I'm making? Is there something I have to do to "reset" the Canvas before drawing?

Thanks for the help!
Eric
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use the debugger and put a breakpoint at the beginning of this sub to make sure that the string received is what you expect.

Here are two slightly different versions of your code:
B4X:
Sub DrawLights(Lights As String)
   Dim rc, yc, gc As Int
   If Lights.SubString2(0, 1) = "R" Then rc = Colors.Red Else rc = Colors.Black
   If Lights.SubString2(1, 2) = "Y" Then yc = Colors.Yellow Else yc = Colors.Black
   If Lights.SubString2(2, 3) = "G" Then gc = Colors.Green Else gc = Colors.Black
    Canvas1.DrawCircle(40dip, 200dip, 30dip, rc, True, 10dip)
    Canvas1.DrawCircle(40dip, 280dip, 30dip, yc, True, 10dip)
    Canvas1.DrawCircle(40dip, 360dip, 30dip, gc, True, 10dip)
    Activity.Invalidate
End Sub

Sub DrawLights2(Lights() As Boolean) 'array of booleans
   Dim rc, yc, gc As Int
   If Lights(0) Then rc = Colors.Red Else rc = Colors.Black
   If Lights(1) Then yc = Colors.Yellow Else yc = Colors.Black
   If Lights(2) Then gc = Colors.Green Else gc = Colors.Black
    Canvas1.DrawCircle(40dip, 200dip, 30dip, rc, True, 10dip)
    Canvas1.DrawCircle(40dip, 280dip, 30dip, yc, True, 10dip)
    Canvas1.DrawCircle(40dip, 360dip, 30dip, gc, True, 10dip)
    Activity.Invalidate
End Sub
 
Upvote 0

ebsebs

New Member
Licensed User
Longtime User
Erel,

to make sure that the string received is what you expect.

That's pretty much what I'm doing. For testing purposes, I am using 2 text boxes.
I type the 3 letters into the first box, call the subroutine when <Enter> is pressed, and display the string received in the subroutine.
The strings displayed in both boxes are the same, so I assume that the correct string is being sent to the subroutine.

I will step through the subroutine to make sure it follows the path I expect, based on the string received.

Thanks,
Eric
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…