C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Application windows, menus & toolbars »

Graphics - Bouncing,Shrinking and Rotating Balls


Posted Date: 29 Jul 2009    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: JayeshMember Level: Gold    
Rating: 1 out of 5Points: 15



This is a demo of graphics is c#.
It displays a number of revolving and expanding balls that moves in window bouncing on the borders.

The main part of the program is a class 'Ball'.

using System;
using System.Drawing;
namespace WindowsApplication1
{
class Ball
{
Graphics g;
Point center;
int size, curSize;
bool IsToLeft, IsToTop, IsExpanding;
int speed;
Color clr;

static Random r = new Random();
public static Rectangle rct;
int Angle;


public Ball(Graphics g)
{
this.g = g;
this.center = new Point(r.Next() % rct.Width, r.Next() % rct.Height);

this.clr = Color.FromArgb(255, r.Next() % 255, r.Next() % 255, r.Next() % 255);
this.size = r.Next() % 100 + 10;
this.curSize = r.Next() % this.size + 1;
IsToLeft = (r.Next() % 2 == 0);
IsToTop = (r.Next() % 2 == 0);
IsExpanding = (r.Next() % 2 == 0);
Angle = r.Next() % 360;
speed = r.Next()%(size/5)+2;

}
void Draw(Color c)
{



Pen pn = new Pen(c);
pn.Width = 5;
g.DrawEllipse(pn, center.X - curSize / 2, center.Y - curSize / 2, curSize, curSize);

Point pt = new Point();
double nCos= Math.Cos(Angle * Math.PI / 180);
double nSin = Math.Sin(Angle * Math.PI / 180);


pt.X = center.X + (int)(curSize * nCos/2);
pt.Y = center.Y + (int) (curSize* nSin/2);
g.DrawLine(pn, pt.X, pt.Y, center.X, center.Y);



}

public void Move()
{

Draw(Color.Black);
CheckHit();
if (IsToLeft)
center.X -= speed;
else
center.X += speed;

if (IsToTop)
center.Y -= speed;
else
center.Y += speed;

if (IsExpanding)
curSize+=size/10;
else
curSize-=size/10;

Angle+=15;

Draw(clr);
}



public void CheckHit()
{
if (rct.Left >= center.X - curSize / 2)
IsToLeft = false;

if (rct.Top >= center.Y - curSize / 2)
IsToTop = false;

if (rct.Right <= center.X + curSize )
IsToLeft = true;
if (rct.Bottom <= center.Y + curSize )
IsToTop = true;

if (curSize <= size/4)
IsExpanding = true;
if (curSize >= size)
IsExpanding = false;
}
}
}



And in the form page, we create an array of ball objects and moves all the balls using a timer.

public partial class Form1 : Form
{
Ball[] ar;
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
ar = new Ball[10];
Ball.rct = new Rectangle(0, 0, Width, Height);

Random r = new Random();

Graphics g = this.CreateGraphics();
for (int i = 0; i < ar.Length;i++ )
{
ar[i] = new Ball(g);
}
}



private void timer1_Tick(object sender, EventArgs e)
{

foreach (Ball b in ar)
{
b.Move();
}
}
}


I am attaching the whole project.





Attachments

  • WindowsApplication1.csproj (30875-292256-WindowsApplication1.csproj)
  • All files - Zipped (30875-42352-Balls-WindowsApplications.rar)
  • Form1.resx (30875-292254-Form1.resx)
  • Form1.cs (30875-292252-Form1.cs.txt)
  • Ball.cs (30875-292254-Ball.cs.txt)
  • Form1.Designer.cs (30875-292255-Form1.Designer.cs.txt)
  • Program.cs (30875-292255-Program.cs.txt)


  • Responses

    Author: Deepika Haridas    30 Jul 2009Member Level: Diamond   Points : 1
    Hi,

    Your attachments cannot be downloaded. Check again.

    --
    Thanks & Regards,
    Deepika
    Editor


    Author: soniumesh    30 Jul 2009Member Level: Gold   Points : 1
    Hi,

    This is good but you can try this in web application also.


    Reagrd,
    umesh soni
    Ahmedabad


    Author: Neeraj Kumar SIngh    09 Aug 2009Member Level: Silver   Points : 1
    hi,

    nice code.
    do we use it in web application?

    Regards,
    Neeraj singh


    Author: Abhay    13 Aug 2009Member Level: Diamond   Points : 1
    Hi jayesh,

    good one .

    keep it up.

    contribute more.

    Thanks and Regards
    Abhay


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Windows graphics in c#  .  Drawing in c#  .  C# graphics  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Generation of controls in Vb.net
    Previous Resource: Create your own Web Browser
    Return to Discussion Resource Index
    Post New Resource
    Category: Application windows, menus & toolbars


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use