Click here to Skip to main content
6,292,426 members and growing! (11,076 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Beginner

Simple Ball Animation with border collision using VS 2005 and .NET 2.0

By Zakaria Bin Abdur Rouf

A very primitive article on simple animation of balls using double buffering technique.
C#, VB 6, .NET, WinXPVS2005, Dev
Posted:12 May 2006
Views:22,141
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 1.68 Rating: 2.40 out of 5
2 votes, 40.0%
1

2

3
1 vote, 20.0%
4
2 votes, 40.0%
5

Sample Image - BallAnimation.jpg

Introduction

In this project I have demonstrated a simple technique on how we can animate some balls in a Picture Box. The balls collides with the walls and change directions. Using the double buffering of .NET 2.0 it is very easy to handle large processing of graphical animation.

I have created a Ball class which can be reused and extended for other projects as well. The Ball class has the following methods: 

DrawBall(Graphics g) 
DrawBall(Graphics g, int move_x, int move_y) 
MoveBall(Ball ball, int screenHeight, int screenWidth)

The DrawBall with the single parameter draws an ellipse on the Graphics object. On the other hand the Drawball with three parameters draws the updated ball on the screen.

The ball is created using the following code:

g.DrawEllipse(new Pen(_ballColor), _x, _y, _width, _height);
// Create solid brush. 
SolidBrush redBrush = new SolidBrush(_ballColor); 
// Fill ellipse on screen
g.FillEllipse(redBrush, _x, _y, _width, _height);

The double buffering is achieved by the following code:

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
this.SetStyle(ControlStyles.UserPaint, true); 

We have used a timer with interval 1 milisecond to animate the balls. In the Timer Tick method I used:
for (int i = 0; i < TOTAL_BALLS; i++) 
{
    manyBall[i].MoveBall(
       manyBall[i],
       pictureBox1.Height, 
       pictureBox1.Width
    ); 
} 
this.Refresh();            
To Refresh the picture box on where we are painting the balls I wrote:
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{
    for(int i = 0; i < TOTAL_BALLS; i++) 
    { 
        manyBall[i].DrawBall(e.Graphics); 
    } 
}

Conclusion


We have seen how we can animate balls using simple animation techniques. Hope you like the code. Till then Eat Bytes Regularly for good health.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Zakaria Bin Abdur Rouf


Member
Zakaria has a keen interest in developing new solutions and exploring new technologies. He has a wide experience in web technologies and business solutions.

His latest project includes Library Management System for University of Dhaka using ASP.NET and Sql Server.

Zakaria completed Masters in Information Technology from Institute of Information Technology http://iit.univdhaka.edu/,
University of Dhaka, had his BSc in Computer Science from AIUB (www.aiub.edu).
School and college from SKBZBIS (www.skbzbis.com)

Latest Project : www.alamgroupae.com
for Alam Group of Companies, Abu Dhabi

Some of his Completed projects:

www.zakasoft.com
www.musaffah.info
www.shonpapri.com
www.skbzbis.com
www.bdembuae.org (The official website of Embassy of the People's Republic of Bangladesh, Abu Dhabi, United Arab Emirates)

Bangla Translation of the Holy Quran (C#/Access)
Library Management System (ASP.NET/C#/SQLServer)
Seat Reservation System (ASP.NET/C#/SQLServer)

Besides programming, he has published his first poetry book (na bola kothamala/Unspoken Words) and in the process of writing several new novels.

Founded ZakaSoft, a division of ZCom, Providing state-of-the-art solutions in United Arab Emirates.

Zakaria Lives in Abu Dhabi and enjoys his spare time in writing and music.

Occupation: Web Developer
Location: Bangladesh Bangladesh

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralInteresting and useful PinmemberBigWorld22:17 12 Jan '09  
GeneralWhy a PictureBox? PinmemberRobert Rohde5:28 13 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 May 2006
Editor:
Copyright 2006 by Zakaria Bin Abdur Rouf
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project