23.3.5.1 Analogue clock

You will also encounter the clock presented in this project in other chapters. Now, however, the focus is on drawing the clock and its hands.

C

Figure 23.3.5.1.1: It's always later than you think …

The source code is structured in such a way that you can easily recognise the interaction of the individual procedures:

[1] ' Gambas class file
[2] 
[3] Public Sub Form_Open()
[4]   FMain.Center
[5]   FMain.Resizable = False  
[6]   lblDayOfWeek.Foreground = Color.DarkGray 
[7] 
[8] ' Analogue clock
[9]   Uhr.Delay = 1000 ' 1s-Takt 
[10]   Uhr.Start()
[11]   Uhr.Trigger()
[12]   
[13]   lblDayOfWeek.Text = SetDayOfWeek()
[14] ' Print System.FirstDayOfWeek '-- For control
[15]   
[16] End
[17] 
[18] '-------------------------------------------------------------------------------------
[19] 
[20] Public Sub Draw_Uhr() '-- Draws the clock face and the three hands
[21]   Dim hZiffernblatt As Image
[22]   Dim x, y As Single
[23]   Dim iRadius As Integer
[24]   Dim Winkel As Float
[25]   Dim W, H As Float
[26]   Dim dNow As Date
[27]   Dim sTime As String
[28] 
[29]   W = dwTest.Width
[30]   H = dwTest.Height
[31]   
[32]   hZiffernblatt = Image.Load("Images/ziffernblatt.png")
[33]   Paint.DrawImage(hZiffernblatt, 0, 0, W, H) '-- Draws the clock face image   
[34]   
[35]   iRadius = 90 ' Basis-Länge eines Zeigers
[36]   dNow = Now   ' Aktuelle Zeit  
[37]   
[38] ' Hour hand
[39]   Winkel = Hour(dNow) * 30 - 90 + 0.5 * Minute(dNow) '-- Angle of the hour hand in degrees
[40]   x = (iRadius - 20) * Cos(Winkel * Pi / 180)        '-- Coordiante x, conversion 1: angle in radians
[41]   y = (iRadius - 20) * Sin(Rad(Winkel))              '-- Coordiante y, conversion 2: Angle in radians
[42]   Zeichnen(x, y, Color.Blue, 5, W, H, sTime)         '-- Coordiante y, conversion 2: Angle in radians
[43]  
[44] ' Minute hand
[45]   Winkel = Minute(dNow) * 6 - 90
[46]   x = (iRadius - 3) * Cos(Rad(Winkel))
[47]   y = (iRadius - 3) * Sin(Rad(Winkel))
[48]   Zeichnen(x, y, Color.Green, 3, W, H, sTime)
[49]     
[50] ' Second hand
[51]   Winkel = Second(dNow) * 6 - 90 ' -90°, as the angle at 3 o'clock is 0° - reference axis!
[52]   x = iRadius * Cos(Rad(Winkel))
[53]   y = iRadius * Sin(Rad(Winkel))
[54]   Zeichnen(x, y, Color.Red, 1, W, H, sTime)
[55] 
[56] End
[57] 
[58] Private Function Zeichnen(x As Float, y As Float, hColor As Integer, iLW As Integer, W As Float, H As Float, sTime As String) As Integer
[59]   ' Draw pointer
[60]     Paint.Brush = Paint.Color(hColor)
[61]     Paint.LineWidth = iLW ' Zeiger-Breite
[62]     Paint.LineCap = Paint.LineCapRound        
[63]     Paint.MoveTo(W / 2, H / 2) ' → Centre of the DrawingArea
[64]     Paint.RelLineTo(x, y)  
[65]     Paint.Stroke() ' Draws a pointer
[66]   
[67]   ' Centre circle (black)
[68]     Paint.Brush = Paint.Color(Color.Black)
[69]     Paint.Arc(W / 2, H / 2, 8) ' Radius = 8
[70]     Paint.Fill()
[71]   
[72]   ' Centre circle (white)
[73]     Paint.Brush = Paint.Color(Color.White)
[74]     Paint.Arc(W / 2, H / 2, 6) ' Radius = 6
[75]     Paint.Fill()
[76]   
[77]   ' Centre (rot) 
[78]     Paint.Brush = Paint.Color(Color.Red)
[79]     Paint.Arc(W / 2, H / 2, 4) ' Radius = 4
[80]     Paint.Fill()
[81]     
[82] End
[83] 
[84] Public Sub Uhr_Timer() ' Timer is called up every second
[85] ' Redraws the current content of the DrawArea by calling dwTest_Draw() with .Refresh
[86]   dwTest.Refresh 
[87]   If Hour(Now) = 23 And Minute(Now) = 59 And Second(Now) = 59 Then
[88]      SetDayOfWeek()
[89]   Endif
[90] End
[91] 
[92] Public Sub dwTest_Draw() 
[93]   Draw_Uhr ' Recalculation and display of the hands
[94] End
[95] 
[96] Public Function SetDayOfWeek() As String
[97]   Dim aTagesListe As String[]
[98]   
[99] ' Determination of weekday and (formatted) date
[100]   aTagesListe = Split("Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag", ",")  
[101]   Return aTagesListe[WeekDay(Now())] & " - " & Format(Now, "dd. mmmm yyyy")
[102]   
[103] End

Comment:

Download

Chapter & Projects

Download