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.
AttachmentsWindowsApplication1.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)
|
| Author: Deepika Haridas 30 Jul 2009 | Member Level: Diamond Points : 1 |
Hi,
Your attachments cannot be downloaded. Check again.
-- Thanks & Regards, Deepika Editor
|
| Author: soniumesh 30 Jul 2009 | Member 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 2009 | Member Level: Silver Points : 1 |
hi,
nice code. do we use it in web application?
Regards, Neeraj singh
|
| Author: Abhay 13 Aug 2009 | Member Level: Diamond Points : 1 |
Hi jayesh,
good one .
keep it up.
contribute more.
Thanks and Regards Abhay
|