B4J Code Snippet [B4X] DrawCircle to SelectedDay in B4XDateTemplate

My personal view, the rectangle is ugly. 😅
So I changed it to circle. ;) nice!

One line of code!​


B4XDateTemplate:
Private Sub DrawBox (c As B4XCanvas, clr As Int, x As Int, y As Int)
    'Dim r As B4XRect
    'r.Initialize(x * boxW, y * boxH, x * boxW + boxW,  y * boxH + boxH)
    'c.DrawRect(r, clr, True, 1dip)
    c.DrawCircle(x * boxW + 0.5 * boxW, y * boxH + 0.5 * boxH, boxH * 0.5, clr, True, 1dip)
End Sub

1777982410736.png
 

aeric

Expert
Licensed User
Longtime User
Improvement:
1. Modify Rectangle to Square
2. Add gap to separate 2 rows
3. Can choose Filled

DrawBox:
Private Sub DrawBox (c As B4XCanvas, clr As Int, Filled As Boolean, x As Int, y As Int)
    Dim r As B4XRect
    Dim stroke As Float = 2dip
    Dim gap As Float = 4dip
    Dim delta As Int = IIf(boxW >  boxH, (boxW - boxH) / 2, 0)
    r.Initialize(x * boxW + delta/2 + gap, y * boxH - delta/2 + gap, x * boxW + boxW - delta - gap/2, y * boxH + boxH - gap/2)
    c.DrawRect(r, clr, Filled, stroke)
End Sub

DrawCirle:
Private Sub DrawCirle (c As B4XCanvas, clr As Int, Filled As Boolean, x As Int, y As Int)
    Dim stroke As Float = 2dip
    Dim gap As Float = 2dip
    c.DrawCircle(x * boxW + boxW/2, y * boxH + boxH/2, boxH/2 - gap, clr, Filled, stroke)
End Sub

Example:
DrawDays:
Private Sub DrawDays
    lblMonth.Text = months.Get(month - 1)
    lblYear.Text = year
    SetYearsButtonState
    cvs.ClearRect(cvs.TargetRect)
    cvsBackground.ClearRect(cvsBackground.TargetRect)
    Dim firstDayOfMonth As Long = DateUtils.setDate(year, month, 1) - 1
    dayOfWeekOffset = (7 + DateTime.GetDayOfWeek(firstDayOfMonth) - FirstDay) Mod 7
    daysInMonth = DateUtils.NumberOfDaysInMonth(month, year)
   
    ' Test highlight on 1st of month
    Dim HighlightDay As Int = 1
    DrawCirle(cvs, xui.Color_Red, False, (HighlightDay - 1 + dayOfWeekOffset) Mod 7, _
            (HighlightDay - 1 + dayOfWeekOffset) / 7)
   
    ' Test highlight on 12th of month
    Dim HighlightDay As Int = 12
    DrawBox(cvs, xui.Color_Magenta, True, (HighlightDay - 1 + dayOfWeekOffset) Mod 7, _
            (HighlightDay - 1 + dayOfWeekOffset) / 7)   
   
    If year = selectedYear And month = selectedMonth Then
        'draw the selected box
        'DrawBox(cvs, SelectedColor, (selectedDay - 1 + dayOfWeekOffset) Mod 7, _
        '    (selectedDay - 1 + dayOfWeekOffset) / 7)
        DrawCirle(cvs, SelectedColor, True, (selectedDay - 1 + dayOfWeekOffset) Mod 7, _
            (selectedDay - 1 + dayOfWeekOffset) / 7)
    End If
   
    Dim daysFont As B4XFont = xui.CreateDefaultBoldFont(14)
    For day = 1 To daysInMonth
        Dim row As Int = (day - 1 + dayOfWeekOffset) / 7
        cvs.DrawText(day, (((dayOfWeekOffset + day - 1) Mod 7) + 0.5) * boxW, _
            (row + 0.5)* boxH + vCorrection, daysFont, DaysInMonthColor , "CENTER")
    Next
    cvsBackground.Invalidate
    cvs.Invalidate
End Sub

1778051940824.png
 
Last edited:
Top