| Author |
Message |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
Posted: Wed Nov 05, 2003 1:41 am Post subject: Create an Analog Clock |
|
|
create an analog clock in your language of choice.
this might be a nice way to compare various languages with each other and investigate each cons and pros The specialty of the house ? thats me (cheap as always)
Visual Basic Explorer - For all your Visual Basic 6 needs! Programmer's Corner - .NET, Web Development, SQL, C++, and more.Edited by: Hrqls at: 11/6/03 8:55 am
|
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
|
| Back to top |
|
 |
theraje Cap'n Arr!
Joined: 22 Oct 2004 Posts: 262
|
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
Posted: Thu Nov 06, 2003 6:01 am Post subject: Re: Create and Analog Clock |
|
|
not ??? hmm have been nice though .. certainly to post all the graphics on here
can noone do it with code ? in any language ? vb6 (busy on it ), vb.net ?, java ?, c++ ?, c# ?, ... ? The specialty of the house ? thats me (cheap as always)
Visual Basic Explorer - For all your Visual Basic 6 needs! Programmer's Corner - .NET, Web Development, SQL, C++, and more. |
|
| Back to top |
|
 |
theraje Cap'n Arr!
Joined: 22 Oct 2004 Posts: 262
|
Posted: Thu Nov 06, 2003 8:12 am Post subject: Re: Create and Analog Clock |
|
|
| I'm pretty sure there's code for such a thing floating around somewhere on the VBE forums... if not, PSC will most assuredly have it. |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
|
| Back to top |
|
 |
theraje Cap'n Arr!
Joined: 22 Oct 2004 Posts: 262
|
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
|
| Back to top |
|
 |
Eric Idea Hamster
Joined: 21 Oct 2004 Posts: 679 Location: Bangor, Maine
|
Posted: Mon Nov 10, 2003 1:20 pm Post subject: Re: Create and Analog Clock |
|
|
All you have to do is poll the time. Easy in VB using the Time function... and most other languages will have an equivalent.
Then just compute the hands (hour and minute). No one said anything about seconds.
I might try something in VB. I don't know any graphical stuff in C# or Java yet...
Eric D. Burdo
Programmers Corner - Tutorials, Source Code, Articles and Tips on programming in VB, VB.Net, C#, and C/C++ the drop ship guide - The complete guide on to how to get started with wholesale drop shipping.
|
|
| Back to top |
|
 |
ModusPonens if A then B, if B then C...
Joined: 22 Oct 2004 Posts: 363 Location: Baltimore
|
Posted: Tue Nov 11, 2003 1:20 pm Post subject: Re: Create and Analog Clock |
|
|
I would not be able to post this without the help of Charles Petzold and his book Programming Microsoft Windows in C# published by Microsoft Press. I tried to make it fancy by having the second hand produce some neat graphics every other minute, so make sure you watch the clock more than two minutes to get the whole effect. Place this code in your Form1.cs and compile.
C#
using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows.Forms;class AnalogClock: Form{        ClockControl theclock;        public static void Main()        {                Application.Run(new AnalogClock());        }        public AnalogClock()        {                WindowState = FormWindowState.Maximized;                theclock = new ClockControl();                theclock.Parent = this;                theclock.Time = DateTime.Now;                theclock.Dock = DockStyle.Fill;                theclock.BackColor = Color.Black;                theclock.ForeColor = Color.Gold;                Timer timer = new Timer();                timer.Interval = 100;                timer.Tick += new EventHandler(TimerOnTick);                timer.Start();        }        void TimerOnTick(object obj, EventArgs ea)        {                theclock.Time = DateTime.Now;        }} class ClockControl: UserControl{        DateTime dt;        public ClockControl()        {                ResizeRedraw = true;                Enabled = false;        }        void InitializeCoordinates(Graphics clockdraw)        {                if (Width == 0 || Height == 0)                        return;                clockdraw.TranslateTransform(Width / 2, Height / 2);                float fInches = Math.Min(Width / clockdraw.DpiX, Height / clockdraw.DpiY);                clockdraw.ScaleTransform(fInches * clockdraw.DpiX / 2000,                        fInches * clockdraw.DpiY / 2000);        }         void DrawDots(Graphics clockdraw, Brush brush)        {                for (int i = 0; i < 60; i++)                {                        int iSize = i % 5 == 0 ? 80 : 10;                        clockdraw.FillRectangle(brush, 0 - iSize /2, -900 - iSize /2,                                 iSize, iSize);                        clockdraw.RotateTransform(6);                }         }         protected virtual void DrawHourHand(Graphics clockdraw, Pen pen)        {                GraphicsState gs = clockdraw.Save();                clockdraw.RotateTransform(360f * Time.Hour / 12 +                        30f * Time.Minute / 60);                pen.Width = 15;                clockdraw.DrawPolygon(pen, new Point[]                                                        {                                                                new Point(0, 150), new Point( 60, 0),                                                                new Point(0, -525), new Point(-60, 0)                                                        });                clockdraw.Restore(gs);        }         protected virtual void DrawMinuteHand(Graphics clockdraw, Pen pen)        {                GraphicsState gs = clockdraw.Save();                clockdraw.RotateTransform(360f * Time.Minute / 60 +                        6f * Time.Second / 60);                pen.Width = 15;                clockdraw.DrawPolygon(pen, new Point[]                                                        {                                                                new Point(0, 200), new Point( 50, 0),                                                                new Point(0, -745), new Point(-50, 0),                });                clockdraw.Restore(gs);        }        protected virtual void DrawSecondHand(Graphics clockdraw, Pen pen)        {                GraphicsState gs = clockdraw.Save();                pen.Width = 1;                clockdraw.RotateTransform(360f * Time.Second / 60 + 6f * Time.Millisecond / 1000);                float minute = DateTime.Now.Minute;                float i = minute/2 - Convert.ToInt16(minute/2);                if (i == 0)                {                        pen.Width = 15;                        clockdraw.DrawLine(pen, 0, 0, 0, -800);                }                                        else                {                        pen.Color = Color.Blue;                        pen.Width = 1;                        clockdraw.DrawLine(pen, 0, 0, 0, -790);                }                                clockdraw.Restore(gs);         }         protected override void OnPaint(PaintEventArgs pea)        {                Graphics clockdraw = pea.Graphics;                Pen pen = new Pen(Color.Gold);                Brush brush = new SolidBrush(Color.Gold);                InitializeCoordinates(clockdraw);                DrawDots(clockdraw, brush);                DrawHourHand(clockdraw, pen);                DrawMinuteHand(clockdraw, pen);                DrawSecondHand(clockdraw, pen);        }        private void InitializeComponent()        {                //                 // ClockControl                //                 this.Name = "ClockControl";                this.Load += new System.EventHandler(this.ClockControl_Load);        }        private void ClockControl_Load(object sender, System.EventArgs e)        {                        }                public DateTime Time        {                get                 {                         return dt;                 }                set                 {                         Graphics clockdraw = CreateGraphics();                        InitializeCoordinates(clockdraw);                        System.DateTime moment = new System.DateTime(                                1999, 1, 13, 3, 57, 32, 11);                        Pen pen = new Pen(BackColor);                         if (dt.Hour != value.Hour)                        {                                DrawHourHand(clockdraw, pen);                        }                         if (dt.Minute != value.Minute)                        {                                DrawHourHand(clockdraw, pen);                                DrawMinuteHand(clockdraw, pen);                        }                         if (dt.Second != value.Second)                        {                                DrawMinuteHand(clockdraw, pen);                                DrawSecondHand(clockdraw, pen);                        }                         if (dt.Millisecond != value.Millisecond)                        {                                DrawSecondHand(clockdraw, pen);                        }                         dt = value;                        pen = new Pen(Color.Gold);                                                DrawHourHand(clockdraw, pen);                        DrawMinuteHand(clockdraw, pen);                                                                        DrawSecondHand(clockdraw, pen);                        clockdraw.Dispose();                }        } } Programmer's Corner - .NET, Web Development, SQL, C++, and more. |
|
| Back to top |
|
 |
SnipeKaT Samurai++
Joined: 22 Oct 2004 Posts: 94
|
Posted: Tue Nov 11, 2003 1:59 pm Post subject: Re: Create and Analog Clock |
|
|
all forms of Basic I know and DOS: You can't unless it's not the right time. |
|
| Back to top |
|
 |
ModusPonens if A then B, if B then C...
Joined: 22 Oct 2004 Posts: 363 Location: Baltimore
|
Posted: Thu Nov 13, 2003 11:52 am Post subject: Re: Create and Analog Clock |
|
|
Here's my VB6 analog clock. Since I wrote this one from scratch without the aid of a book and it's more than just a little routine I took credit for it in the source code. Place this code in a form and enjoy.
Option Explicit'__________________________________________''Analog Clock''by Mark Schweikert'modusponens@toadmail.com'November 13, 2003'__________________________________________Public X As LongPublic Y As LongPublic Z As IntegerPublic degree As DoublePublic radiusX As LongPublic radiusY As LongPublic convert As DoublePublic cSize As IntegerPublic uhand As LinePublic ClockCenterX As IntegerPublic ClockCenterY As IntegerPublic WithEvents ClockTimer As TimerConst PI = 3.14159265358979Private Sub DrawDial() radiusX = 1500 radiusY = 1500 For Z = 1 To 60 '6 degrees per minute marker '30 degrees per hour marker degree = Z * 6 convert = PI / 180 'from Radian to Degree X = ClockCenterX - (Sin(-degree * convert) * radiusX) Y = ClockCenterY - (Sin((90 + (degree)) * convert) * radiusY) 'draw small circle for minute markers and 'larger circle for hour markers If Z Mod 5 = 0 Then cSize = 35 Else cSize = 15 Circle (X, Y), cSize, &HFFFFFF Next 'draw circle in center for visual reference Circle (ClockCenterX, ClockCenterY), 30End SubPrivate Sub Form_Load() 'Setup form With Form1 .Height = 4400 .Width = 4400 .Caption = "Analog Clock" .AutoRedraw = True .BackColor = &HFF8080 End With 'Create hands Me.Controls.Add "VB.line", "LineSecond" With Me!LineSecond .BorderStyle = 1 .BorderColor = &H0 .BorderWidth = 1 .DrawMode = 5 .X1 = 1 .Y1 = 1 .X2 = 1 .Y2 = 1 .Visible = True End With Me.Controls.Add "VB.line", "LineMinute" With Me!LineMinute .BorderStyle = 1 .BorderColor = &HFF0000 .BorderWidth = 2 .DrawMode = 13 .X1 = 1 .Y1 = 1 .X2 = 1 .Y2 = 1 .Visible = True End With Me.Controls.Add "VB.line", "LineHour" With Me!LineHour .BorderStyle = 1 .BorderColor = &HFF0000 .BorderWidth = 3 .DrawMode = 13 .X1 = 1 .Y1 = 1 .X2 = 1 .Y2 = 1 .Visible = True End With 'Create and initialize timer Set ClockTimer = Controls.Add("vb.timer", "ClockTimer") ClockTimer.Interval = 1000 'Draw clock DrawDial UpdateHand ("Second") UpdateHand ("Minute") UpdateHand ("Hour") End SubPrivate Sub Form_Resize() Me.Cls ClockCenterX = (Form1.Width / 2.1) ClockCenterY = (Form1.Height / 2.25)End SubPrivate Sub ClockTimer_Timer() DrawDial UpdateHand ("Second") UpdateHand ("Minute") UpdateHand ("Hour") End SubPrivate Function UpdateHand(Hand As String) If Hand = "Second" Then Set uhand = Me!LineSecond radiusX = 1360 radiusY = 1360 '6 degrees per second degree = Second(Now) * 6 End If If Hand = "Minute" Then Set uhand = Me!LineMinute radiusX = 1350 radiusY = 1350 '6 degrees per minute + fraction of minute to 'increment mintue hand toward next minute marker degree = Minute(Now) * 6 + ((Second(Now) + 1) / 12) End If If Hand = "Hour" Then Set uhand = Me!LineHour radiusX = 800 radiusY = 800 '30 degrees per minute + fraction of hour to 'increment hour hand toward next hour marker degree = (Hour(Now) * 360 / 12) + ((Minute(Now)) / 2) End If convert = 3.14159265358979 / 180 'from radians to degrees 'calculate position of hand tip and reposition X = ClockCenterX - (Sin(-degree * convert) * radiusX) Y = ClockCenterY - (Sin((90 + (degree)) * convert) * radiusY) uhand.X1 = ClockCenterX uhand.Y1 = ClockCenterY uhand.X2 = X uhand.Y2 = YEnd Function
Note: maybe we shouldn't allow emoticons in this forum, I always forget to turn them off. Programmer's Corner - .NET, Web Development, SQL, C++, and more.Edited by: modusponen at: 11/14/03 9:12 am
|
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
|
| Back to top |
|
 |
ModusPonens if A then B, if B then C...
Joined: 22 Oct 2004 Posts: 363 Location: Baltimore
|
Posted: Thu Nov 13, 2003 12:55 pm Post subject: Re: Create and Analog Clock |
|
|
Thanks, I was surprised that it was easier to program in C# than VB6. C# has a rotate function which makes drawing the hands a snap but VB6 doesn't (or at least I didn't find it).
In the Form_Load I forgot to set the form auto-redraw to True, I'll edit and add that in. Also you can't set the borderstyle at run-time, I find that really annoying.
Also you can delete these declarations: Public CenterX As Long Public CenterY As Long I used them during development and they are no longer needed. (Never mind I did it already) Programmer's Corner - .NET, Web Development, SQL, C++, and more.Edited by: modusponen at: 11/14/03 9:12 am
|
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
|
| Back to top |
|
 |
Eric Idea Hamster
Joined: 21 Oct 2004 Posts: 679 Location: Bangor, Maine
|
|
| Back to top |
|
 |
ModusPonens if A then B, if B then C...
Joined: 22 Oct 2004 Posts: 363 Location: Baltimore
|
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 558 Location: Alkmaar, The Netherlands
|
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2002 phpBB Group
|