Home

Graphics Example: Chasing Circles

     

Introduction

This is an example of drawing circles in a C# application.

When the application, a black form fills the whole screen. It draws a small circle in the center. Then five circles starts drawing and chasing each other, around the central circle.

Drawing Circles

Practical LearningPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio and create a new Windows  Forms Application named ChasingCircles
  2. Change the properties of the form as follows:
    BackColor: Black
    FormBorderStyle: None
    WindowState: Maximized
  3. Click the middle of the form and, in the Properties window, click the Events button
  4. In the Events section of the Properties window, double-click KeyDown
  5. Implement the event as follows:
    private void Exercise_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
            Close();
    }
  6. Return to the form and add a Timer control to it
  7. Set the timer's Enabled to True and its Interval to 50
  8. Double-click the timer and change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ChasingCircles
    {
        public partial class Form1 : Form
        {
            int angle1 = 0;
            int angle2 = 15;
            int angle3 = 30;
            int angle4 = 45;
            int angle5 = 60;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                Graphics graph = Graphics.FromHwnd(this.Handle);
    
                graph.TranslateTransform((float)(this.ClientSize.Width / 2),
                                         (float)(this.ClientSize.Height / 2));
    
                int radius = 250;
    
                double PI2 = 2 * Math.PI;
    
                int X1 = (int)(radius * Math.Cos(angle1 / PI2));
                int Y1 = (int)(radius * Math.Sin(angle1 / PI2));
                int X2 = (int)(radius * Math.Cos(angle2 / PI2));
                int Y2 = (int)(radius * Math.Sin(angle2 / PI2));
                int X3 = (int)(radius * Math.Cos(angle3 / PI2));
                int Y3 = (int)(radius * Math.Sin(angle3 / PI2));
                int X4 = (int)(radius * Math.Cos(angle4 / PI2));
                int Y4 = (int)(radius * Math.Sin(angle4 / PI2));
                int X5 = (int)(radius * Math.Cos(angle5 / PI2));
                int Y5 = (int)(radius * Math.Sin(angle5 / PI2));
    
                graph.Clear(Color.Black);
    
                graph.DrawEllipse(Pens.Fuchsia, -50, -50, 100, 100);
                graph.DrawEllipse(Pens.Aqua, X1 - 200, Y1 - 200, 400, 400);
                graph.DrawEllipse(Pens.Yellow, X2 - 200, Y2 - 200, 400, 400);
                graph.DrawEllipse(Pens.Blue, X3 - 200, Y3 - 200, 400, 400);
                graph.DrawEllipse(Pens.White, X4 - 200, Y4 - 200, 400, 400);
                graph.DrawEllipse(Pens.Red, X5 - 200, Y5 - 200, 400, 400);
    
                angle1++;
                angle2++;
                angle3++;
                angle4++;
                angle5++;
            }
    
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Escape)
                    Close();
            }
        }
    }
  9. Execute the application
 

Home Copyright © 2009 C# Key