Home

GDI+ Circle Animation: Central Circle

Introduction

This a simple circular animatoin where an empty circle is drawn from the center of a form and starts enlarging by the seconds. When its diameter becomes equal to the width (and/or height) of the form, its starts shrinking back.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    PictureBox pbxCanvas;
    private Timer tmrDrawObjects;

    bool isMax;
    int mainRadius;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        pbxCanvas = new PictureBox();
        pbxCanvas.Dock = DockStyle.Fill;
        pbxCanvas.Paint += new PaintEventHandler(pbxCanvasPaint);
        Controls.Add(pbxCanvas);

        tmrDrawObjects = new Timer();
        tmrDrawObjects.Interval = 20;
        tmrDrawObjects.Enabled = true;
        tmrDrawObjects.Tick += new EventHandler(tmrDrawObjectsTick);

        Text = "Circle Animation";
        Load += new EventHandler(FormLoad);

        MaximizeBox = false;
        StartPosition = FormStartPosition.CenterScreen;
        ClientSize = new System.Drawing.Size(1024, 1024);
    }

    private void FormLoad(object sender, EventArgs e)
    {
        mainRadius = 10;
    }

    private void DrawCentralCircle(Graphics graph, int Radius)
    {
        int start = (pbxCanvas.ClientRectangle.Width / 2) - Radius;
        int end = (pbxCanvas.ClientRectangle.Height / 2) - Radius;
        int diam = Radius * 2;

        graph.DrawEllipse(new Pen(Color.Orange), start, end, diam, diam);
    }

    private void pbxCanvasPaint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, Width, Height);

        // Central Circle
        DrawCentralCircle(e.Graphics, mainRadius);
    }

    private void tmrDrawObjectsTick(object sender, EventArgs e)
    {
        if (isMax == true)
            mainRadius--;
        else
            mainRadius++;

        if (mainRadius > (pbxCanvas.ClientRectangle.Height / 2))
            isMax = true;
        if (mainRadius < 10)
            isMax = false;

        pbxCanvas.Invalidate();
    }

    [STAThread]
    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());
        return 0;
    }
}
 

Home Copyright © 2016-2017, FunctionX Home