Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i used to develope an analog clock

here is the code

but it creates problems


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

  
namespace ana1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        public interface IClockModel
        {
            #region ClockDataEventArgs
            event EventHandler<ClockDataEventArgs> ClockChanging;
            event EventHandler<ClockDataEventArgs> ClockChanged;
            void SetClockInterval(TimeSpan timeSpan);
            IClockModel Start();
            IClockModel Continue();
            IClockModel Stop();
            ClockData ClockData { get; set; }
            bool IsRunning { get; }

        }

        private void DrawMarkers()
        {
            MarkerCanvas.Children.Clear();
            for (int i = 0; i < 60; ++i)
            {
                Rectangle markerRectangle = new Rectangle();
                if (i % 5 == 0)
                {
                    markerRectangle.Width = 3;
                    markerRectangle.Height = 8;
                    markerRectangle.Fill = new SolidColorBrush(Colors.White);
                    markerRectangle.RenderTransform = this.CreateTransformGroup(i * 6,
                        -(markerRectangle.Width / 2),
                        -(markerRectangle.Height * 2 + 4.5 - ClockFaceEllipse.StrokeThickness / 2 - this.clockHeight / 2));
                }
                else
                {
                    markerRectangle.Width = 1;
                    markerRectangle.Height = 4;
                    markerRectangle.Fill = new SolidColorBrush(Colors.White);
                    markerRectangle.RenderTransform = this.CreateTransformGroup(i * 6,
                        -(markerRectangle.Width / 2),
                        -(markerRectangle.Height * 2 + 12.75 - ClockFaceEllipse.StrokeThickness / 2 - this.clockHeight / 2));
                }
                MarkerCanvas.Children.Add(markerRectangle);
            }
        }
        private TransformGroup CreateTransformGroup(double angle, double firstTranslateXValue, double firstTranslateYValue)
        {
            TransformGroup transformGroup = new TransformGroup();
            TranslateTransform firstTranslate = new TranslateTransform();
            firstTranslate.X = firstTranslateXValue;
            firstTranslate.Y = firstTranslateYValue;
            transformGroup.Children.Add(firstTranslate);
            RotateTransform rotateTransform = new RotateTransform();
            rotateTransform.Angle = angle;
            transformGroup.Children.Add(rotateTransform);
            TranslateTransform secondTranslate = new TranslateTransform();
            secondTranslate.X = this.clockWidth / 2;
            secondTranslate.Y = this.clockHeight / 2;
            transformGroup.Children.Add(secondTranslate);
            return transformGroup;
        }
        private void DrawSecondsHand(ClockData clockData)
        {
            if (secondsHand != null && ClockHandsCanvas.Children.Contains(secondsHand)) ClockHandsCanvas.Children.Remove(secondsHand);
            secondsHand = this.CreateHand(this.secondsHandWidth, this.secondsHandHeight, new SolidColorBrush(Colors.Black), 0, 0, 0, null);
            secondsHand.RenderTransform = this.CreateTransformGroup(
            clockData.Seconds * 6, -this.secondsHandWidth / 2, -this.secondsHandHeight + 4.25);
            if (ClockHandsCanvas.Children.Contains(secondsHand) == false)
                ClockHandsCanvas.Children.Add(secondsHand);

        }

        public void Update(ClockData clockData)
        {
            this.DrawSecondsHand(clockData);
            this.DrawMinutesHand(clockData);
            this.DrawHoursHand(clockData);

        }
        public abstract class ClockBase : IClockModel
        {
            private ClockData clockData;
            protected bool isRunning;
            protected DispatcherTimer timer;
            public event EventHandler<ClockDataEventArgs> ClockChanging;
            public event EventHandler<ClockDataEventArgs> ClockChanged;
            public ClockBase()
            {
                clockData = new ClockData();
                timer = new DispatcherTimer();
                timer.Tick += new EventHandler(timer_Tick);
                this.SetClockInterval(new TimeSpan(0, 0, 0, 0, 1));
            }
            public virtual void SetClockInterval(TimeSpan timeSpan)
            {
                timer.Interval = timeSpan;
            }
            public ClockData ClockData
            {
                get
                {
                    return this.clockData;
                }
                set
                {
                    this.OnClockChangind(new ClockDataEventArgs(this.clockData));
                    this.clockData = value;
                    this.OnClockChanged(new ClockDataEventArgs(this.clockData));
                }
            }
            public bool IsRunning
            {
                get
                {
                    return this.isRunning;
                }
            }
            public abstract IClockModel Start();
            public abstract IClockModel Stop();
            public abstract IClockModel Continue();
            protected abstract void UpdateClockData(DateTime dateTime);
            protected virtual void OnClockChangind(ClockDataEventArgs e)
            {
                EventHandler<ClockDataEventArgs> temp = this.ClockChanging;
                if (temp != null)
                    temp(this, e);
            }
            protected virtual void OnClockChanged(ClockDataEventArgs e)
            {
                EventHandler<ClockDataEventArgs> temp = this.ClockChanged;
                if (temp != null)
                    temp(this, e);
            }
            private void timer_Tick(object sender, EventArgs e)
            {
                this.OnClockChangind(new ClockDataEventArgs(this.ClockData));
                this.UpdateClockData(DateTime.Now);
                this.OnClockChanged(new ClockDataEventArgs(this.ClockData));
            }
        }
        public class Clock : ClockBase
        {
            public static IClockModel Create()
            {
                return new Clock();
            }
            public override IClockModel Start()
            {
                if (IsRunning)
                    return this;
                timer.Start();
                isRunning = true;
                return this;
            }
            public override IClockModel Stop()
            {
                timer.Stop();
                isRunning = false;
                return this;
            }



            public override IClockModel Continue()
            {
                return this.Start();
            }



            protected override void UpdateClockData(DateTime dateTime)
            {
                this.ClockData.Update(dateTime);
            }

        }

        public class ClockPresenter
        {
            public Callback Response;
            public ClockPresenter(IClockView clockView, IClockModel clockModel)
            {
                this.ClockView = clockView;
                this.ClockModel = clockModel;
                this.ClockView.StartClock += new EventHandler(ClockView_StartClock);
                this.ClockView.StopClock += new EventHandler(ClockView_StopClock);
                this.ClockModel.ClockChanged += new EventHandler<ClockDataEventArgs>(ClockModel_ClockChanged);
            }
            public IClockView ClockView
            {
                get;
                set;
            }
            public IClockModel ClockModel
            {
                get;
                set;
            }



            private void ClockView_StopClock(object sender, EventArgs e)
            {
                this.ClockModel.Stop();
            }



            private void ClockView_StartClock(object sender, EventArgs e)
            {
                this.ClockModel.Start();
            }



            private void ClockModel_ClockChanged(object sender, ClockDataEventArgs e)
            {
                Callback temp = this.Response;
                if (temp != null)
                    temp(e.ClockData);
            }
        }

        //IClockModel clockModel = Clock.Create();

        //clockModel.ClockData = new ClockData().Update( DateTime.Now );



        //ClockPresenter presenter = new ClockPresenter( this.AnalogClockControl, clockModel );

        //presenter.Response += new Callback( this.AnalogClockControl.Update );



        //clockModel.Start();  }


    }


[snip]
Posted
Updated 12-Feb-10 23:37pm
v5

I'm at a loss. You asked if there was a clock namespace, when you'd already written all of this ? Now, you post the code and say it has 'problems', but not what they are, what line has issues, if there's an error, or what it is you want us to do ?

Do you not want help ?

You posted all this code, yet you didn't tell us the app is WPF, or give us the XAML if someone was dumb enough to try to work out your problems when you refuse to tell us what they are ?
 
Share this answer
 
v2
cshuhh wrote:
but it creates problems


Could you post the problems here (or the errors you get so that someone is able to help you)?
 
Share this answer
 
Ah - that explains what an idiot this guy has been. He asked twice how to write a clock ( he asked if there's a clock namespace.... ), then he posted all this code.

To the OP - don't be so stupid. Buy a book and read it, you can't expect to download random code and understand it without knowing about the languages you're using first. And, tag your questions properly in future.
 
Share this answer
 
Contact details for the author of the code were provided in the original articles. He'll be more able to answer questions than any of us.
http://www.silverlightshow.net/items/Developing-Silverlight-Analog-Clock-pattern-oriented-approach.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900