Hello i am trying to make analog clock.
And i want to split my classes. my control class in which is analog clock painting functions and etc... And i want to split this class and to make timer class which would be used by my analogClock class. Is it possible?
That is my code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace AnalogClockControl
{
public class AnalogClock : UserControl
{
public float PI = 3.141592654F; // PI konstanta
private Taimer timerKintamasis; <-- that is parameter by which i am trying to access other class "Taimer"
public DateTime dateTime; // public dateTime kintamasis datai fiksuot
float fRadius; // laipsniai
float fCenterX; // centro koordinates
float fCenterY; // centro koordinates
float fCenterCircleRadius; // centro circle laipsniai
float fHourLength; // valandos ilgis
float fMinLength; // minuciu ilgis
public float fSecLength; // sekundžiu ilgis
float fHourThickness; // valandos rodykles storio parametras
float fMinThickness; // minutes rodykles storio parametras
public float fSecThickness; // sekundess rodykles storio parametras
//chronometro rodykle
public float rodykl;
//žadintuvo rodykle
public float alarmRod;
// Valandiniu brukšniu liniju paišymas
bool bDraw5MinuteTicks = true;
// 60 minuciu brukšniu liniju paišymas
bool bDraw1MinuteTicks = true;
// Tiksejimu rodykliu storis
float fTicksThickness = 1;
// Žadintuvo rodykle parametrai
public string valandos;
// Žadintuvo rodykles parametrai
public string minutes;
Color hrColor = Color.DarkMagenta; // Valandos rodykles spalva
Color minColor = Color.Green; // Minuciu rodykles spalva
Color secColor = Color.Red; // Sekundžiu rodykles spalva
Color circleColor = Color.Red; // Vidurio circle spalva
Color ticksColor = Color.White; // Sekundiniu Tiksejimu rodykles spalva
public Timer timer1; // Laikrodžio taimeris <-- that is timer which i want to move to 'Taimer' class and this class would call it...
public Timer timer2; // Chronometro taimeris
public Timer timer3; // Žadintuvo taimeris
public IContainer components; // Konteineris
// Laikrodyje naudojamu raidžiu/skaitmenu šriftas
Font f = new Font("Calibri", 14, FontStyle.Bold);
public AnalogClock()
{
this.timerKintamasis = timerKintamasis;
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timerKintamasis.timer1 = new System.Windows.Forms.Timer(this.timerKintamasis.components); <- this is call.. new object..
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.timer3 = new System.Windows.Forms.Timer(this.components);
//
// timer1 parametrai (Laikrodžio)
//
//this.timer1.Enabled = true; <-
//this.timer1.Interval = 1000; <- all this stuff would be in 'Taimer' class
//this.timer1.Tick += new System.EventHandler(this.timer1_Tick); <- in constructor there could be just timer1 = enabled.
//
// analoginio laikrodžio sukurimas
//
this.Name = "AnalogClock";
this.Resize += new System.EventHandler(this.AnalogClock_Resize);
this.Load += new System.EventHandler(this.AnalogClock_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.AnalogClock_Paint);
this.Visible = false;
}
#endregion
// Laikrodžio užsikrovimas
private void AnalogClock_Load(object sender, System.EventArgs e)
{
dateTime = DateTime.Now;
this.AnalogClock_Resize(sender, e);
}
// Kiekvieno Tiksejimo valdymas
public void timer1_Tick(object sender, System.EventArgs e)
{
this.dateTime = DateTime.Now;
this.Refresh(); // sitas vis perpaiso
// this.Invalidate();
}
//// Laikrodžio Taimerio valdymas
//public void Start()
//{
// timer1.Enabled = true;
// this.Refresh();
//}
//// Laikrodžio Taimerio valdymas
//public void Stop()
//{
// timer1.Enabled = false;
//}
// Sekundine rodykle
private void DrawLine(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(color, fThickness),
fCenterX - (float)(fLength / 9 * Math.Sin(fRadians)),
fCenterY + (float)(fLength / 9 * Math.Cos(fRadians)),
fCenterX + (float)(fLength * Math.Sin(fRadians)),
fCenterY - (float)(fLength * Math.Cos(fRadians)));
}
// Chronometro rodykle
public void DrawLine2(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(color, fThickness),
fCenterX - (float)(fLength / 5 * Math.Sin(fRadians)),
fCenterY + (float)(fLength / 5 * Math.Cos(fRadians)),
fCenterX + (float)(fLength * Math.Sin(fRadians)),
fCenterY - (float)(fLength * Math.Cos(fRadians)));
}
public void ChronometroRodyklFunkcija(object sender, EventArgs e)
{
rodykl += 1 / 10F * PI / 180 * 6;
}
public void AlarmClockFunkcija(object sender, EventArgs e)
{
this.dateTime = DateTime.Now;
this.Refresh();
}
// Valandine ir Minutine rodykles
private void DrawPolygon(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
PointF A = new PointF((float)(fCenterX + fThickness * 2 * Math.Sin(fRadians + PI / 2)),
(float)(fCenterY - fThickness * 2 * Math.Cos(fRadians + PI / 2)));
PointF B = new PointF((float)(fCenterX + fThickness * 2 * Math.Sin(fRadians - PI / 2)),
(float)(fCenterY - fThickness * 2 * Math.Cos(fRadians - PI / 2)));
PointF C = new PointF((float)(fCenterX + fLength * Math.Sin(fRadians)),
(float)(fCenterY - fLength * Math.Cos(fRadians)));
PointF D = new PointF((float)(fCenterX - fThickness * 4 * Math.Sin(fRadians)),
(float)(fCenterY + fThickness * 4 * Math.Cos(fRadians)));
PointF[] points = { A, D, B, C }; // išdestymas
e.Graphics.FillPolygon(new SolidBrush(color), points); //spalvinimas
}
// Laikrodžio nupaisymas
private void AnalogClock_Paint(object sender, PaintEventArgs e)
{
float fRadHr = (dateTime.Hour % 12 + dateTime.Minute / 60F) * 30 * PI / 180;
float fRadMin = (dateTime.Minute) * 6 * PI / 180;
float fRadSec = (dateTime.Second) * 6 * PI / 180;
DrawPolygon(this.fHourThickness, this.fHourLength, hrColor, fRadHr, e);
DrawPolygon(this.fMinThickness, this.fMinLength, minColor, fRadMin, e);
DrawLine(this.fSecThickness, this.fSecLength, secColor, fRadSec, e);
DrawLine2(this.fSecThickness, this.fSecLength, Color.Blue, alarmRod, e);
DrawLine3(this.fSecThickness, this.fSecLength, Color.Chartreuse, rodykl, e);
for (int i = 0; i < 60; i++) // 60 minuciu
{
for (int j = 1; j <= 12; j++) // valandu skaiciu paišymas
{ // 2.8
e.Graphics.DrawString("" + j, f, Brushes.Red,
Width / 2 + (int)(Width / 3.5 * Math.Sin(j * Math.PI / 6))
- (int)e.Graphics.MeasureString("" + j, f).Width / 2,
Height / 2 - (int)(Height / 3.5 * Math.Cos(j * Math.PI / 6))
- (int)e.Graphics.MeasureString("" + j, f).Height / 2);
}
// Paišo kiekvienos valandos liniju brukšnius
if (this.bDraw5MinuteTicks == true && i % 5 == 0)
{
e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
fCenterX + (float)(this.fRadius / 1.50F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.50F * Math.Cos(i * 6 * PI / 180)),
fCenterX + (float)(this.fRadius / 1.65F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.65F * Math.Cos(i * 6 * PI / 180)));
}
else if (this.bDraw1MinuteTicks == true) // paišo minuciu liniju brukšnius
{
e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
fCenterX + (float)(this.fRadius / 1.50F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.50F * Math.Cos(i * 6 * PI / 180)),
fCenterX + (float)(this.fRadius / 1.55F * Math.Sin(i * 6 * PI / 180)),
fCenterY - (float)(this.fRadius / 1.55F * Math.Cos(i * 6 * PI / 180)));
}
}
//Paišo circle viduryje
e.Graphics.FillEllipse(new SolidBrush(circleColor), fCenterX - fCenterCircleRadius / 2, fCenterY - fCenterCircleRadius / 2, fCenterCircleRadius, fCenterCircleRadius);
}
public void AlarmClockLine()
{
if (Convert.ToInt32(valandos) > 23 || Convert.ToInt32(minutes) > 59)
{
valandos = "0";
minutes = "0";
}
alarmRod = (Convert.ToInt32(valandos) % 12 + Convert.ToInt32(minutes) / 60F) * 30 * PI / 180;
}
// Alarm clock rodykle
public void DrawLine3(float fThickness, float fLength, Color color, float fRadians, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(color, fThickness),
fCenterX - (float)(fLength / 9 * Math.Sin(fRadians)),
fCenterY + (float)(fLength / 9 * Math.Cos(fRadians)),
fCenterX + (float)(fLength * Math.Sin(fRadians)),
fCenterY - (float)(fLength * Math.Cos(fRadians)));
}
// Matmenu keitimas
private void AnalogClock_Resize(object sender, EventArgs e)
{
this.Draw1MinuteTicks = true;
this.Draw5MinuteTicks = true;
this.ForeColor = System.Drawing.SystemColors.AppWorkspace;
this.HourHandColor = System.Drawing.Color.DarkGray;
this.Location = new System.Drawing.Point(12, 12);
this.MinuteHandColor = System.Drawing.Color.DarkGray;
this.Name = "analogClock3";
this.SecondHandColor = System.Drawing.Color.BlueViolet;
this.Size = new System.Drawing.Size(402, 402);
this.TabIndex = 2;
this.TicksColor = System.Drawing.Color.Black;
this.Width = this.Height;
//this.Width = Width/2;
//this.Height = Height/2;
this.fRadius = this.Height / 2;
this.fCenterX = this.ClientSize.Width / 2;
this.fCenterY = this.ClientSize.Height / 2;
this.fHourLength = (float)this.Height / 3 / 1.65F;
this.fMinLength = (float)this.Height / 3 / 1.20F;
this.fSecLength = (float)this.Height / 3 / 1.15F;
this.fHourThickness = (float)this.Height / 100;
this.fMinThickness = (float)this.Height / 150;
this.fSecThickness = (float)this.Height / 200;
this.fCenterCircleRadius = this.Height / 50;
this.Refresh();
}
public Color HourHandColor // Valandines rodykles spalva
{
get { return this.hrColor; }
set { this.hrColor = value; }
}
public Color MinuteHandColor // minutines rodykles spalva
{
get { return this.minColor; }
set { this.minColor = value; }
}
public Color SecondHandColor // sekundines rodykles spalva
{
get { return this.secColor; }
set
{
this.secColor = value;
this.circleColor = value;
}
}
public Color TicksColor // tiksejimu rodykles spalva
{
get { return this.ticksColor; }
set { this.ticksColor = value; }
}
public bool Draw1MinuteTicks // geteriai seteriai minutinems linijoms
{
get { return this.bDraw1MinuteTicks; }
set { this.bDraw1MinuteTicks = value; }
}
public bool Draw5MinuteTicks // geteriai seteriai valandinems linijoms
{
get { return this.bDraw5MinuteTicks; }
set { this.bDraw5MinuteTicks = value; }
}
}
}
Loading
Audrius GriPosted Dec 1, 2011, 5:14 PM
Sam HobbsPosted Dec 1, 2011, 4:58 PM
Audrius GriPosted Dec 1, 2011, 4:18 PM
The clock is made of two projects... One is for paintings (Windows form app) and other for control -> User Control library, there are methods and etc. I have created 'Taimer' class.. I don't know if there must inherit UserControl or not.. I just made public partial class Taimer : UserControl.... And there i need to put all timers of my program. There are 3 timers. One for just tickings of the clock.. SecondsLine ticking on the clock... second timer is for chronometer... start chronometer button = start timer.. stop chronometer = stop timer... continue chronometer = continue timer... and the last timer is for alarm clock.. Alarm clock time is set and the timer I don't know if there it is needed or not, every second checking DateTime.Now == setTime... So in my timer class i need to put all those timers that would be used in the AnalogClock (main program).. My teacher required that everything must be splitted, not in the one class.. so my form class is startup class and main class AnalogClock do functionality, starts timer and begin ticking, but i need that it would be calling timer from 'Taimer' class.....
I tried to explain everything... Is it clearer?
Sam HobbsPosted Dec 1, 2011, 3:57 PM
So you want Enabled and Interval to be members of your class. I think you are also saying you want to have a Tick event handler. What do you need for the Tick event handler to do? I assume it will do something different for each instance of your timer class. So you will need to provide a delegate so that your users of your timer class will be able to provide the code for what to do. When you are all done, I think the new class will not make your code easier or anything but perhaps I just do not know what you want to make your timer class do.
Another important descision is whether you will derive from the .Net Timer class or if your timer class will have a .Net Timer class embedded in it.