Home

GDI+ Circle Animation: 5 Circles

Introduction

This drawing contains 5 circles: one central empty circle and 4 circles around (left, top, right, and bottom sides). The central circle increases up to the width of the form and decreases down to around to close the center of the form. The small circles around have a fixed diameter but also stay attached to the borders of the central circle.

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 = 100;
    }

    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);
    }

    void DrawBorderingCircle(Graphics graph, Color color, int left, int top)
    {
        graph.DrawEllipse(new Pen(color), left, top, 40, 40);
    }

    void DrawLeftCircle(Graphics graph)
    {
        graph.DrawEllipse(new Pen(Color.White), (pbxCanvas.ClientRectangle.Width / 2) - mainRadius - 20, (pbxCanvas.ClientRectangle.Height / 2) - 20, 40, 40);
    }

    void DrawTopCircle(Graphics graph)
    {
        graph.DrawEllipse(new Pen(Color.Green), (pbxCanvas.ClientRectangle.Width / 2) - 20, (pbxCanvas.ClientRectangle.Height / 2) - mainRadius - 20, 40, 40);
    }

    void DrawRightCircle(Graphics graph)
    {
        graph.DrawEllipse(new Pen(Color.Red), (pbxCanvas.ClientRectangle.Width / 2) + mainRadius - 20, (pbxCanvas.ClientRectangle.Height / 2) - 20, 40, 40);
    }

    void DrawBottomCircle(Graphics graph)
    {
        graph.DrawEllipse(new Pen(Color.Yellow), (pbxCanvas.ClientRectangle.Width / 2) - 20, (pbxCanvas.ClientRectangle.Height / 2) + mainRadius - 20, 40, 40);
    }

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

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

        // Left Circle
        DrawBorderingCircle(e.Graphics, Color.White, (pbxCanvas.ClientRectangle.Width / 2) - mainRadius - 20, (pbxCanvas.ClientRectangle.Height / 2) - 20);
        //DrawLeftCircle(e.Graphics);
        // Top Circle
        DrawBorderingCircle(e.Graphics, Color.Green, (pbxCanvas.ClientRectangle.Width / 2) - 20, (pbxCanvas.ClientRectangle.Height / 2) - mainRadius - 20);
        //DrawTopCircle(e.Graphics);
        // Right Circle
        DrawBorderingCircle(e.Graphics, Color.Red, (pbxCanvas.ClientRectangle.Width / 2) + mainRadius - 20, (pbxCanvas.ClientRectangle.Height / 2) - 20);
        //DrawRightCircle(e.Graphics);
        // Bottom Circle
        DrawBorderingCircle(e.Graphics, Color.Yellow, (pbxCanvas.ClientRectangle.Width / 2) - 20, (pbxCanvas.ClientRectangle.Height / 2) + mainRadius - 20);
        //DrawBottomCircle(e.Graphics);
    }

    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