using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
using Views;
public class StopWatchwithViews {
///
/// The Stop Watch with Views Bishop and Horspool March 2003
/// =========================
/// Implements a stopwatch
/// Shows the integration of animation with Views.
///
Views.Form f;
Thread clockThread;
void SetUp() {
f = new Views.Form(
@"
");
// c is the reference to the actual control variable
// whose name is listed as f["pic"] in Views
// add the event
f["pic"].c.Paint +=
new PaintEventHandler(new Clock().ClockPaint);
// create and start a thread to control the clock
clockThread = new Thread(new ThreadStart(this.TickClock));
clockThread.Start();
}
bool done = false;
void Go() {
SetUp();
while (!done) {
string c = f.GetControl();
if (c == null) break;
ActionPerformed(c);
}
f.CloseGUI();
}
void ActionPerformed(string c) {
switch (c) {
case "Start" :
clockThread.Start();
break;
case "Resume" :
clockThread.Resume();
break;
case "Pause" :
clockThread.Suspend();
break;
case "End" :
clockThread.Suspend();
done = true;
break;
default :
break;
}
}
void TickClock() {
while (true) {
f["pic"].c.Invalidate();
Thread.Sleep(10);
}
}
public class Clock {
int seconds = 0;
SolidBrush bluebrush, whitebrush;
Pen blackpen;
int start = 100;
public Clock() {
blackpen = new Pen(Color.Black);
bluebrush = new SolidBrush(Color.Blue);
whitebrush = new SolidBrush(Color.LightGray);
}
// The handler redraws the whole clock
public void ClockPaint(Object source, PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawEllipse(blackpen,start/2,start/2,100,100);
g.FillEllipse(whitebrush,start/2,start/2,100,100);
g.FillEllipse(bluebrush,start,start,5,5);
Hands(g);
seconds++;
}
void Hands(Graphics g) {
double hourAngle = 2*Math.PI*(seconds - 3*60*60)/(12*60*60);
double minuteAngle = 2*Math.PI*(seconds - 15*60)/(60*60);
double secondAngle = 2*Math.PI*(seconds - 15)/60;
g.DrawLine(blackpen, start, start,
start + (int)(30*Math.Cos(hourAngle)),
start + (int)(30*Math.Sin(hourAngle))+5);
g.DrawLine(blackpen,start, start,
start + (int)(40*Math.Cos(minuteAngle)),
start + (int)(40*Math.Sin(minuteAngle)));
g.DrawLine(blackpen,start, start,
start + (int)(45*Math.Cos(secondAngle)),
start + (int)(45*Math.Sin(secondAngle)));
}
}
static void Main() {
new StopWatchwithViews().Go();
}
}