11 Replies - 22442 Views - Last Post: 04 July 2013 - 10:55 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Challenge: Timepieces

Post icon  Posted 07 February 2012 - 03:38 PM

Challenge: Timepieces

Challenge

Simply build a clock. It's can be as simple as you like, or as complicated as you.

Examples
Binary Clock
Analogue Console Clock




Entrants
  • Frameworks required.
  • vb.net submissions are preferred.
  • SourceCode must be in spoilered code tags.
    Spoiler

    If you don't want to reveal your code, then don't submit a submission.
  • If you like you can include a Zip-File of the project / executable.


Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Timepieces

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Challenge: Timepieces

Posted 08 February 2012 - 01:52 PM

OK, I'm going to spend as much time describing this as I spent making it. I'm not proud of it but I thought I'd play around with something completely crazy.

I cheated, I used the DrawPie method because I could just set the angle and go. Really not much to look at but I think it show just have creative you can be with .Net

Hope you get a laugh out of this. It should at the very least show you how little I know about drawing on a form.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,159
  • Joined: 12-June 08

Re: Challenge: Timepieces

Posted 08 February 2012 - 01:56 PM

Side note - if it helps anyone else out my 'knobs' tutorial might be helpful for drawing on a form. ;)
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Challenge: Timepieces

Posted 08 February 2012 - 02:19 PM

:sigh:
Duly noted :D

I've not had much use for doing it, I just realized with this exercise that I really am not very good at it.

I'll give your tut a read and maybe see about improving that slop I submitted.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,159
  • Joined: 12-June 08

Re: Challenge: Timepieces

Posted 08 February 2012 - 02:26 PM

*In my Peppermint Larry voice* Hmm, yes.. why don't give my knobs a view! */voice*
Was This Post Helpful? 0
  • +
  • -

#6 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Challenge: Timepieces

Posted 15 February 2012 - 05:21 AM

View PostCharlieMay, on 08 February 2012 - 01:52 PM, said:

OK, I'm going to spend as much time describing this as I spent making it. I'm not proud of it but I thought I'd play around with something completely crazy.

I cheated, I used the DrawPie method because I could just set the angle and go. Really not much to look at but I think it show just have creative you can be with .Net

Hope you get a laugh out of this. It should at the very least show you how little I know about drawing on a form.
Spoiler

nice and short code
but seems a little bug there
when it run's second hand disappear when it pass the hour hand,minute hand

This post has been edited by thava: 15 February 2012 - 05:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Challenge: Timepieces

Posted 17 February 2012 - 02:58 PM

Oh yea, there's plenty of things that could be improved. This was just something I was messing with in the spirit of the challenge. I might try messing with it again but I don't have much use for a clock :D

One of the things that bugs me the most is that I should really move the hour hand in relation to where the minute hand is at. So for example at 4:30 the hour hand should be halfway between the 4 and the 5. This code only changes that position when the hour moves to the next number (in this case 5). Hell, for that matter, having the hour labels and minute ticks on the dam clock would be an improvement.

I don't do a lot with graphics but I wanted something different to submit so this is what I came up with. If I had more time I would love to take some of the ideas in modi123_1's knob tutorial and create "Pretty" objects and rotate them according to the time.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Challenge: Timepieces

Posted 24 February 2012 - 11:58 AM

I thought about making the clock from it's constituent components.
Clock
Public Class Clock
  Public Event ClockChanged(ByVal o As Object, e As ClockChangesArg)
  Dim WithEvents p As New Pendulum(TimeSpan.FromSeconds(1.0))
  Dim WithEvents s As New SecondHand(0)
  Dim WithEvents m As New MinuteHand(0)
  Dim WithEvents h As New HourHand(1)
  Public Sub New()
  End Sub
  Public Sub HandleHandMovement(o As Object, e As ClockHandEventArg) Handles h.Movement, m.Movement, s.Movement
    Select Case True
      Case TypeOf o Is SecondHand : If e.Prev = 59 AndAlso e.Current = 0 Then m.Advance()
      Case TypeOf o Is MinuteHand : If e.Prev = 59 AndAlso e.Current = 0 Then h.Advance()
    End Select
    RaiseEvent ClockChanged(Me, New ClockChangesArg(h, m, s, p))
  End Sub
  Public Sub HandleTicksAndTocks() Handles p.Tick, p.Tock
    s.Advance()
  End Sub
  Public Sub Start()
    p.StartSwing()
  End Sub
End Class


Events Args
Public Class ClockChangesArg
  Inherits EventArgs
  Private _H As HourHand
  Private _M As MinuteHand
  Private _S As SecondHand
  Private _P As Pendulum
  Public Sub New(H As HourHand, M As MinuteHand, S As SecondHand, P As Pendulum)
    _H = H : _M = M : _S = S : _P = P
  End Sub
  Public ReadOnly Property H As HourHand
    Get
      Return _H
    End Get
  End Property
  Public ReadOnly Property M As MinuteHand
    Get
      Return _M
    End Get
  End Property
  Public ReadOnly Property S As SecondHand
    Get
      Return _S
    End Get
  End Property
  Public ReadOnly Property P As Pendulum
    Get
      Return _P
    End Get
  End Property
End Class


Clock Hand EventArgs
Public Class ClockHandEventArg
  Inherits EventArgs
  Private _Prev As Double
  Private _Current As Double
  Public Sub New(Prev As Double, Current As Double)
    _Prev = Prev : _Current = Current
  End Sub
  Public ReadOnly Property Prev As Double
    Get
      Return _Prev
    End Get
  End Property

  Public ReadOnly Property Current As Double
    Get
      Return _Current
    End Get
  End Property

End Class


Clock Hand
Public MustInherit Class ClockHand
  Private _Value, _Advancement, _Maximum, _Minimum As Double
  Public Event Movement(src As Object, e As ClockHandEventArg)
  Protected Friend Sub New(Value As Double, Advancement As Double, Maximum As Double, Minimum As Double)
    _Value = Value : _Advancement = Advancement : _Maximum = Maximum : _Minimum = Minimum
  End Sub
  Public ReadOnly Property Value() As Double
    Get
      Return _Value
    End Get
  End Property
  Public ReadOnly Property Max() As Double
    Get
      Return _Maximum
    End Get
  End Property
  Public ReadOnly Property Min() As Double
    Get
      Return _Minimum
    End Get
  End Property
  Public Sub Advance()
    Dim prev = _Value
    _Value += _Advancement
    If _Value > _Maximum Then _Value = 0.0
    RaiseEvent Movement(Me, New ClockHandEventArg(prev, _Value))
  End Sub
End Class



Minute Hand
Public Class MinuteHand
  Inherits ClockHand
  Public Sub New(Value As Double)
    MyBase.New(0, 1, 59, 0)
  End Sub
End Class


Second Hand
Public Class SecondHand
  Inherits ClockHand
  Public Sub New(Value As Double)
    MyBase.New(0, 1, 59, 0)
  End Sub
End Class


Hour Hand
Public Class HourHand
  Inherits ClockHand
  Public Sub New(Value As Double)
    MyBase.New(12, 1, 12, 1)
  End Sub
End Class



Pendulum
Public Class Pendulum
  Public Enum States
    Tick
    Tock
  End Enum
  Private _period As TimeSpan
  Private _State As States
  Private t As Threading.Tasks.Task
  Public ReadOnly Property State As States
    Get
      Return _State
    End Get
  End Property
  Private Sub TickTock() Handles Me.Tick, Me.Tock
    If Me.State = States.Tick Then
      t = Task.Factory.StartNew(Sub()
                                  Threading.Thread.Sleep(_period)
                                  _State = States.Tock
                                  RaiseEvent Tock()
                                End Sub)
    ElseIf Me.State = States.Tock Then
      t = Task.Factory.StartNew(Sub()
                                  Threading.Thread.Sleep(_period)
                                  _State = States.Tick
                                  RaiseEvent Tick()
                                End Sub)

    End If
  End Sub
  Public Sub New(period As TimeSpan)
    _period = period
    _State = States.Tock
   End Sub
  Public Sub StartSwing()
    TickTock()
  End Sub
  Public Event Tick()
  Public Event Tock()
End Class
[/b]

Was This Post Helpful? 0
  • +
  • -

#9 Drowjam  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 16-October 12

Re: Challenge: Timepieces

Posted 20 April 2013 - 01:02 AM

Ok this is probably the simplest way to make a clock using VB

Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Challenge: Timepieces

Posted 25 April 2013 - 02:53 PM

Took my 15 minutes for this...Attached File  Clock.zip (89.9K)
Number of downloads: 28
Was This Post Helpful? 0
  • +
  • -

#11 b mitsai  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 09-November 12

Re: Challenge: Timepieces

Posted 04 July 2013 - 10:34 AM

why use timer object?
Why not syncthread?
make a sleep(1000) and then sync the grafic main form.
easy? :)
I'm going to test this out :D
Was This Post Helpful? 0
  • +
  • -

#12 b mitsai  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 59
  • Joined: 09-November 12

Re: Challenge: Timepieces

Posted 04 July 2013 - 10:55 AM

Look i did it!
simple code:

Imports System.Threading.Thread
Public Class Form1
    Private Delegate Sub Delegate1()
    Public H, M, S As Integer
    Public timerThread As New Threading.Thread(AddressOf timer)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        H = DateTime.Now.Hour
        M = DateTime.Now.Minute
        S = DateTime.Now.Second
        Refresh_label()
        timerThread.Start()
    End Sub
    Private Sub Refresh_label()
        Me.Label1.Text = H & ":" & M & ":" & S
    End Sub
    Public Sub timer()
        While True
            Me.Invoke(New Delegate1(AddressOf Refresh_label))
            Sleep(1000)
            '*
            S += 1
            If S = 60 Then
                M += 1
                If M = 60 Then
                    H += 1
                    If H = 24 Then
                        H = 0
                    End If
                    M = 0
                End If
                S = 0
            End If
            '*
        End While
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        timerThread.Abort()
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1