clock graphic

cdoverlaw

Well-known member
Joined
Oct 11, 2003
Messages
146
Hi, i followed a tutorial which generates a clock graphic here is the code:
Code:
Clock
    Sub DrawHand(ByVal myGraphics As Graphics, ByVal myPen As Pen, ByVal cx As Integer, ByVal cy As Integer, ByVal num As Integer, ByVal rad As Integer)

        Dim x, y As Integer

        x = rad * Math.Sin((num * Math.PI) / 30)
        y = rad * Math.Cos((num * Math.PI) / 30)
        myGraphics.DrawLine(myPen, cx, cy, cx + x, cy - y)

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim cx, cy, rad As Integer
        cx = 50 : cy = 50 : rad = (-40)
        Dim myGraphics As System.drawing.Graphics

        myGraphics = pbxClock.CreateGraphics()
        myGraphics.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2, rad * 2)
        myGraphics.DrawEllipse(Pens.DarkBlue, cx - rad, cy - rad, rad * 2, rad * 2)
        DrawHand(myGraphics, Pens.Blue, cx, cy, Hour(Now) * 5, rad * 0.6)
        DrawHand(myGraphics, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
        DrawHand(myGraphics, Pens.Red, cx, cy, Second(Now), rad)
        myGraphics.Dispose()
    End Sub

But the analogue clock shows upside down, how can i fix this
 
Code:
Clock
    Sub DrawHand(ByVal myGraphics As Graphics, ByVal myPen As Pen, ByVal cx As Integer, ByVal cy As Integer, ByVal num As Integer, ByVal rad As Integer)

        Dim x, y As Integer

        x = rad * -Math.Sin((num * Math.PI) / 30)
        y = rad * -Math.Cos((num * Math.PI) / 30)
        myGraphics.DrawLine(myPen, cx, cy, cx + x, cy - y)

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim cx, cy, rad As Integer
        cx = 50 : cy = 50 : rad = (-40)
        Dim myGraphics As System.drawing.Graphics

        myGraphics = pbxClock.CreateGraphics()
        myGraphics.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2, rad * 2)
        myGraphics.DrawEllipse(Pens.DarkBlue, cx - rad, cy - rad, rad * 2, rad * 2)
        DrawHand(myGraphics, Pens.Blue, cx, cy, Hour(Now) * 5, rad * 0.6)
        DrawHand(myGraphics, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
        DrawHand(myGraphics, Pens.Red, cx, cy, Second(Now), rad)
        myGraphics.Dispose()
    End Sub
This is the working code, thanks for the help
 
More than just this code is required, you need a timer (called timer one, enabled and set to have a interval of 1000) , and a picture box (called pbxClock)
 
*sigh*
i really am a newbie..
thanks it works good now..
and now to transform it to a stopwatch with both Analog and digital display and middle times..
*sigh* =)
 
Back
Top