Jump to content
Xtreme .Net Talk

Recommended Posts

Guest double-helix
Posted

Lo People.

 

Well, whats new? Well being the nice person I am, i only just realized the amazing potential of the system.drawing.graphics class. So, being the .NET addict i am, i came up with this analog clock. Be warned, your system will have to be running in AM / PM time, not 24hr for this to work.

 

So, start a new project in Visual Basic, and choose A Windows Application. Create A picturebox, and change the name of it to pbxClock. Then, create a timer control, and leave it as timer1, changing the interval to 1000. Also, make sure the timer is enabled.

 

right - now go into the code view, and type the following:

 

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 - y0

 

End Sub

 

'Note, this next code goes into the tick of the timer control

 

Sub Timer1_Tick()

dim cx, cy, rad as Integer

cx = 100 : cy = 100 : rad - 80

dim myGraphics as system.drawing.Graphics

 

myGraphics = pbxClock.CreateGraphics()

myGraphics.FillEllipse(Brushes.white, cx - rad, cy - rad, rad * 2, rad * 2)

myGraphics.DrawEllipse(Pens.yellow, cx - rad, cy - rad, rad * 2, rad * 2)

DrawHand(myGraphics, Pens.red, cx, cy, Hour(Now) * 5, rad * 0.6)

DrawHand(myGraphics, Pens.green, cx, cy, Minute(Now), rad * 0.9)

DrawHand(myGraphics, Pens.blue, cx, cy, Second(Now), rad)

myGraphics.dispose()

End Sub

 

'Thats All Folks

 

And so, you see how simpleit is to create advanced graphics.

Later people. Email @ irish_kneecaps@hotmail.com :p

Guest Morgon
Posted
Just so you know .. I can only get this to work by using an Overload Handler for Timer1.Tick .. were you able to get it just by using a function?
  • 9 months later...
Posted

Ha! Cool stuff. I'm surprised you typed it all into your post, though. (At least I'm guessing that's what you did, based on the 2 typos?) CTRL+C is you friend. ;)

 

Good work.

zig?
Posted

Just messing about and modified this to create an international TimeZoner.

 


  Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

       Dim cx, cy, rad As Integer
       cx = 100 : cy = 100 : rad = 80
       Dim grpLondon As System.Drawing.Graphics
       Dim grpNewYork As System.Drawing.Graphics
       Dim grpMoscow As System.Drawing.Graphics

       grpLondon = Me.pbxLondon.CreateGraphics()
       grpNewYork = Me.pbxNewYork.CreateGraphics()
       grpMoscow = Me.pbxMoscow.CreateGraphics()

       ' London

       grpLondon.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2, rad * 2)
       grpLondon.DrawEllipse(Pens.Yellow, cx - rad, cy - rad, rad * 2, rad * 2)
       DrawHand(grpLondon, Pens.Red, cx, cy, Hour(Now) * 5, rad * 0.6)
       DrawHand(grpLondon, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
       DrawHand(grpLondon, Pens.Blue, cx, cy, Second(Now), rad)

       ' NewYork

       grpNewYork.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2, rad * 2)
       grpNewYork.DrawEllipse(Pens.Yellow, cx - rad, cy - rad, rad * 2, rad * 2)
       DrawHand(grpNewYork, Pens.Red, cx, cy, Hour(Now.AddHours(-5)) * 5, rad * 0.6)
       DrawHand(grpNewYork, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
       DrawHand(grpNewYork, Pens.Blue, cx, cy, Second(Now), rad)

       ' Moscow

       grpMoscow.FillEllipse(Brushes.White, cx - rad, cy - rad, rad * 2, rad * 2)
       grpMoscow.DrawEllipse(Pens.Yellow, cx - rad, cy - rad, rad * 2, rad * 2)
       DrawHand(grpMoscow, Pens.Red, cx, cy, Hour(Now.AddHours(3)) * 5, rad * 0.6)
       DrawHand(grpMoscow, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
       DrawHand(grpMoscow, Pens.Blue, cx, cy, Second(Now), rad)

       grpLondon.Dispose()
       grpNewYork.Dispose()
       grpMoscow.Dispose()

   End Sub

 

Yes I realise I'm using the wrong prefix for a Graphics object..:D

My website
  • 5 months later...
Posted (edited)

I get the error

Expression is not a method

How do i solve this

 

Edit:

Solved that but the clock is rotated 180 degrees (upside down)

 

Here is my 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.Yellow, cx - rad, cy - rad, rad * 2, rad * 2)
       DrawHand(myGraphics, Pens.Red, cx, cy, Hour(Now) * 5, rad * 0.6)
       DrawHand(myGraphics, Pens.Green, cx, cy, Minute(Now), rad * 0.9)
       DrawHand(myGraphics, Pens.Blue, cx, cy, Second(Now), rad)
       myGraphics.Dispose()
   End Sub

 

Also is it possible to add 4 main numbers (12, 3, 6, 9) to the clock and lines denoting each hour

Edited by cdoverlaw

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...