[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Animate gears in C#

[Animate gears in C#]

The example Draw gears in C# explains how you can draw gears in C#. That example only needs a couple of small changes to animate the gears.

The previous example starts drawing with angle theta = 0 so the first part of the gear is drawn to the right of the gear's center. In this example the DrawGear method takes a start_angle parameter that indicates where drawing should start. The following code shows how the program sets the initial theta value.

// Draw a gear. private void DrawGear(float start_angle, Graphics gr, Brush axle_brush, Brush gear_brush, Pen gear_pen, PointF center, float radius, float tooth_length, int num_teeth, float axle_radius, bool start_with_tooth) { ... // Set theta for the beginning of the first tooth. float theta = start_angle; if (start_with_tooth) theta += dtheta / 2; else theta -= dtheta / 2; ... }

The following code shows how the Paint event handler draws the gears.

// The angle used as the gears' origins. private float StartAngle = 0; private float dStartAngle = (float)(Math.PI / 90); // Draw the gear. private void picGears_Paint(object sender, PaintEventArgs e) { // Draw smoothly. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; const float radius = 50; const float tooth_length = 10; float x = picGears.ClientSize.Width / 2 - radius - tooth_length - 1; float y = picGears.ClientSize.Height / 3; DrawGear(StartAngle, e.Graphics, Brushes.Black, Brushes.LightBlue, Pens.Blue, new PointF(x, y), radius, tooth_length, 10, 5, true); x += 2 * radius + tooth_length + 2; DrawGear(-StartAngle, e.Graphics, Brushes.Black, Brushes.LightGreen, Pens.Green, new PointF(x, y), radius, tooth_length, 10, 5, false); y += 2f * radius + tooth_length + 2; DrawGear(StartAngle, e.Graphics, Brushes.Black, Brushes.Pink, Pens.Red, new PointF(x, y), radius, tooth_length, 10, 5, true); }

This code declares the form-level variable StartAngle. The Paint event handler passes StartAngle as the initial drawing angle for the two gears that should rotate clockwise. It passes -StartAngle for the gear that should rotate counter-clockwise.

To control the animation, the form includes a Timer that ticks every 10 milliseconds while it is enabled. The following code shows the Timer's Tick event handler.

// Increment the gears' start angle and redraw. private void tmrRotate_Tick(object sender, EventArgs e) { StartAngle += dStartAngle; picGears.Refresh(); }

This code simply increments StartAngle and refreshes the picGears PictureBox to make the Paint event handler redraw the gears with their new start angles.

The final piece of new code, which is in the start button's Click event handler, starts or stops the Timer.

// Start or stop the animation. private void btnStartStop_Click(object sender, EventArgs e) { tmrRotate.Enabled = !tmrRotate.Enabled; if (tmrRotate.Enabled) btnStartStop.Text = "Stop"; else btnStartStop.Text = "Start"; }

This code toggles the Timer's Enabled property and then sets the button's text appropriately.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.