Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


Create an Analog Clock
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars
Author Message
Hrqls
De Blonde Puffin


Joined: 22 Oct 2004
Posts: 558
Location: Alkmaar, The Netherlands

PostPosted: Wed Nov 05, 2003 1:41 am    Post subject: Create an Analog Clock Reply with quote

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

PostPosted: Wed Nov 05, 2003 7:52 am    Post subject: Re: Create and Analog Clock Reply with quote

Analog? You mean you are going to make me work with graphics? : Ok...


Programmer's Corner - .NET, Web Development, SQL, C++, and more.
Visual Basic Explorer - For all your Visual Basic 6 needs!

Back to top
theraje
Cap'n Arr!


Joined: 22 Oct 2004
Posts: 262

PostPosted: Wed Nov 05, 2003 5:53 pm    Post subject: Re: Create and Analog Clock Reply with quote

All you have to do is use the line command with the arguments as circular function values (sine/cosine/whatever). No need to open paint and draw a picture for every position the hands can be in. :p

Back to top
Hrqls
De Blonde Puffin


Joined: 22 Oct 2004
Posts: 558
Location: Alkmaar, The Netherlands

PostPosted: Thu Nov 06, 2003 6:01 am    Post subject: Re: Create and Analog Clock Reply with quote

not ??? hmm have been nice though .. certainly to post all the graphics on here :evil

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

PostPosted: Thu Nov 06, 2003 8:12 am    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Thu Nov 06, 2003 8:55 am    Post subject: Re: Create and Analog Clock Reply with quote

hey! thats not the idea of this forum ... you are just being lazy .. not get out of that chair .. eh into that coding chair .. and write something :)


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

PostPosted: Sat Nov 08, 2003 11:37 pm    Post subject: Re: Create and Analog Clock Reply with quote

Yeah, right. I don't know how to get the system time, I don't know how to do the math for the hands... I don't know how to do any of that stuff. :lol

Back to top
Hrqls
De Blonde Puffin


Joined: 22 Oct 2004
Posts: 558
Location: Alkmaar, The Netherlands

PostPosted: Mon Nov 10, 2003 12:33 pm    Post subject: Re: Create and Analog Clock Reply with quote

neither do we :)

the 'war' is to create an analog clock .. it doesnt say it has to be accurate ;)

maybe something inaccurate can be made accurate by other people?


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
Eric
Idea Hamster


Joined: 21 Oct 2004
Posts: 679
Location: Bangor, Maine

PostPosted: Mon Nov 10, 2003 1:20 pm    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Tue Nov 11, 2003 1:20 pm    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Tue Nov 11, 2003 1:59 pm    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Thu Nov 13, 2003 11:52 am    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Thu Nov 13, 2003 12:36 pm    Post subject: Re: Create and Analog Clock Reply with quote

That's a cool clock.. :)


BDI quietly throws out his generic VB6 clock he was working on

Don't worry about the emoticons.... over time, *narf* corrects you enough that it just starts sticking.


Programmer's Corner - .NET, Web Development, SQL, C++, and more.
Visual Basic Explorer - For all your Visual Basic 6 needs!

Edited by: BeadyEye at: 11/13/03 12:36 pm
Back to top
ModusPonens
if A then B, if B then C...


Joined: 22 Oct 2004
Posts: 363
Location: Baltimore

PostPosted: Thu Nov 13, 2003 12:55 pm    Post subject: Re: Create and Analog Clock Reply with quote

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

PostPosted: Fri Nov 14, 2003 8:26 am    Post subject: Re: Create and Analog Clock Reply with quote

damn! ... i wasnt fast enough .. started my own analog clock in vb6 when i started this thread .. but didnt finish (in time) .. ah well .. congrats! (i mean it *humpf!* ;) )

i am curious if anyone can create a simple analog clock in java

that would be very useful to me as i am doing a course in java (i am not at the actual coding yet but this code example would be quite nice for me ;) )

is it also possible to create an analog clock in java script or vbscript ?


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
Eric
Idea Hamster


Joined: 21 Oct 2004
Posts: 679
Location: Bangor, Maine

PostPosted: Fri Nov 14, 2003 9:02 am    Post subject: Re: Create and Analog Clock Reply with quote

Try converting the C# code to Java.... it should be fairly easy, as long as you can get the graphics stuff ported.

And go ahead and do your VB6 clock... we will accept more than one. :)

And, everyone who submitted a sample here should submit it to the Source Code section of Programmers Corner. :)



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

PostPosted: Fri Nov 14, 2003 9:04 am    Post subject: Re: Create and Analog Clock Reply with quote

Is there a reason why other people can't post VB6 because one is already listed? I would find it interesting to see individual styles among programmers using the same language as much as seeing how different languages handle tasks. 8o


Programmer's Corner - .NET, Web Development, SQL, C++, and more.

Back to top
Hrqls
De Blonde Puffin


Joined: 22 Oct 2004
Posts: 558
Location: Alkmaar, The Netherlands

PostPosted: Fri Nov 14, 2003 11:53 am    Post subject: Re: Create and Analog Clock Reply with quote

ok we will :)

(btw i started this thread/war .. so i should be allowed to post mine!!! 8) )

submit it to pc for real ? hmm ... only if its decided to be good enough :)

its the graphical stuff in java that i am interested in the most :)

once i get my infrared connection running on my pc to connect to my phone i plan to play around with java and create some applications/games/... for my phone :)


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
bdi
Nobody


Joined: 21 Oct 2004
Posts: 1646
Location: Chicago

PostPosted: Fri Nov 14, 2003 3:27 pm    Post subject: Re: Create and Analog Clock Reply with quote

Quote:
once i get my infrared connection running on my pc to connect to my phone i plan to play around with java and create some applications/games/... for my phone
Submit those too. :)


Programmer's Corner - .NET, Web Development, SQL, C++, and more.
Visual Basic Explorer - For all your Visual Basic 6 needs!

Back to top
Hrqls
De Blonde Puffin


Joined: 22 Oct 2004
Posts: 558
Location: Alkmaar, The Netherlands

PostPosted: Mon Nov 17, 2003 3:19 am    Post subject: Re: Create and Analog Clock Reply with quote

trust me to do that .... :evil


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
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Code Wars All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 


Powered by phpBB © 2001, 2002 phpBB Group