Drawing a Shape on the C# Windows Form
Animating objects in C# is a fun and practical way to learn graphics programming.
In this tutorial, you'll discover how to move an object across a canvas in C# using simple animation and graphics principles.
We'll show you step by step how to draw, animate, and control object movement in Windows Forms with the Paint event.
Whether you're a beginner learning C# animation or just want to bring your projects to life,
this guide will help you master smooth and efficient object movement on screen.
We begin by creating a circle (our object) using the FillEllipse method from the Graphics class.
This forms the foundation for object movement in C#.
How to Animate an Object in C#
To animate the object, we repeatedly redraw it in new positions while clearing the previous frame.
This creates the illusion of continuous motion - the basis of C# canvas animation.
Steps to Animate a Circle in C#
- Use
Form1 Paint class handles the button click event.
- Use loops or timers to gradually change the object's coordinates.
Create a new C# Windows Forms Application
project
;
call it Dymetric_CS.
Create 2 new C# classes;
Call them Dymetric and MovingBody.
Type out the adjoining C# code for animating an image body across the windows form.
Important: To generate the Form1_Paint C# code stub,
select the form in design mode; go to its properties; click on the
symbol and select Paint.
Note: The C# code for our Form1 Class was too big to be squeezed in with the others.
It is simply included as a link of its own.
How the C# Move Object Animation Code Works
The MovingBody C# class handles the actual drawing and movement. It:
- Draws a circle with
FillEllipse.
- Uses
Clear from Graphics.FromImage to erase old drawings.
- Updates the
x coordinate with a loop.
- Uses
Thread.sleep() to control speed.
This demonstrates a simple but effective C# animation technique.
Frequently Asked Questions About Moving Objects in C#
How can I move an object smoothly in C#?
Use a timer or loop to update object coordinates gradually, redrawing each frame on the canvas for smooth motion.
Can I move objects with keyboard input?
Yes - handle key events and update object position in response to arrow keys or other inputs to create event-based object movement in C#.
Understanding Object Movement in C#
In this tutorial, we've learnt how to move an object across a canvas in C# using the Graphics class.
The idea is to draw a shape (like a circle) and update its position in each frame - a simple yet effective C# animation example.
- Use C# graphics to handle drawing operations.
- Update object position incrementally for smooth animation.
- Handle
Paint events to redraw dynamically.
You've learnt how to move an object in C# using a simple code example. We've used Windows Forms
and the Graphics class to draw and animate a circle - our moving body - across a C# canvas.
To keep the animation smooth, you can use double buffering and a Timer to redraw the moving object without flicker.
Key Takeaways on C# Windows Form and Object Animation
Animating graphics in C# is one of the most exciting ways to understand how programming logic connects with visual design.
In this tutorial, you've learnt how to move an object across a canvas in C# using a simple, beginner-friendly code example.
We've used Windows Forms and the Paint event to show how to draw, clear,
and redraw shapes smoothly - the foundation of any basic C# animation tutorial.
Moving shapes or objects in C# helps students visualize core programming concepts like loops, coordinates, and event handling.
This guide demonstrates how to control speed, direction, and timing to create a natural movement effect. By following each step carefully,
you'll be able to animate shapes in C#, such as circles or rectangles, and make them glide across the screen with precision.
Whether you're a secondary school student, a beginner developer, or simply curious about graphics animation in C#,
this tutorial will help you build confidence in creating visual applications.
By the end, you would have understood how object movement in Windows Forms works and how to extend the technique for games,
data visualizations, or creative projects.
So! C# Fun Practice Exercise - Animate Circle
As a fun practice exercise, try experimenting with shapes, speeds, and directions
to deepen your understanding of object movement in C# graphics.
C# Moving Body Window Display Code Stub
C# Moving Body Code for Dymetric Class
using System.Windows.Forms;
namespace Dymetric
{
class Dymetric
{
private MovingBody move_body;
private bool do_simulation;
public Dymetric(int width, int height)
{
move_body = new MovingBody(width, height);
do_simulation = false;
}
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
move_body.inPlay(e);
do_simulation = false;
}
else
{
move_body.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# Animation Code for Moving Body Class
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class MovingBody
{
private int x, y, previous_x, previous_y;
private const int ballDIAMETER = 80;
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush ball_colour, bg_colour;
public MovingBody(int screen_width, int screen_height)
{
ball_colour = new SolidBrush(Color.Yellow);
bg_colour = new SolidBrush(Color.LightGray);
offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
offscreen_g = Graphics.FromImage(offscreen_bitmap);
offscreen_g.Clear(Color.LightGray);
previous_x = x = 10;
previous_y = y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
public void clearAndDraw(PaintEventArgs e)
{
offscreen_g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER);
offscreen_g.FillEllipse(ball_colour, x, y, ballDIAMETER, ballDIAMETER);
previous_x = x;
previous_y = y;
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
public void inPlay(PaintEventArgs e)
{
while (x < offscreen_bitmap.Width - ballDIAMETER)
{
clearAndDraw(e);
x += 10;
Thread.Sleep(50);
}
x = 10;
y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
}
}