PDA

View Full Version : Circle animation


Xem
03-19-2009, 04:23 PM
Hi, I'm new to C# and I need some help with a little "animation".

I wrote the underlaying logic for drawing circles (listing below) and now I want to show those circles in graphics (on a Form or somewhere else, don't know).

The basic idea is to have a limited number of circles displayed. This means erasing one before drawing a new one. The logic is fairly simple.

So, I really need help with these graphics. I'm still kinda thick-headed on this subject. :p

Additional questions in the listing - commented with /**/.

Thanx!!!

Listing (yes, written for a console output :rolleyes:):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
struct Circle
{
public float x; //circle position x
public float y; ////and y
public float r; //circle radius

public Circle(float x, float y, float r)
{
this.x = x; //init droplet to x
this.y = y; //init droplet to y
this.r = r; //init radius to r
}

public void Show()
{
/*
This is where I want to put graphics coding to draw a blue circle
this.x, this.y, this.r
*/
/*debuging*/Console.WriteLine("Shown x: {0} y: {1} r: {2}", this.x, this.y, this.r);
}

public void Hide()
{
/*
This is where I want to put graphics coding to erase a circle (or to draw a background-colored one)
this.x, this.y, this.r
*/
/*debuging*/Console.WriteLine("Hiden x: {0} y: {1} r: {2}", this.x, this.y, this.r);
}
}

class Program
{
static void Main()
{
int maxX = 100; //max x position
int maxY = 70; //max y position
int maxR = 10; //max radius
int maxC = 10; //max circles
Circle[] C = new Circle[maxC]; //array of circles

int i;
int n = 100; //cycles to do

Random rnd = new Random();

for (i = 0; i < maxC; i++) //it generates first maxC circles and draws them
{
C[i] = new Circle(rnd.Next(maxX), rnd.Next(maxY), rnd.Next(maxR));
C[i].Show();
/* How do I make a pause or wait to slow down the drawing? */
}
i = 0;
do
{
C[i].Hide();

/* Do I need do dispose of C[i] before creating a new one? */

C[i] = new Circle(rnd.Next(maxX), rnd.Next(maxY), rnd.Next(maxR));
C[i].Show();

/* How do I make a pause or wait to slow down the drawing? */
n-=1;
i += 1;
if (i >= maxC) i = 0; //cycling through the array

/*debuging*/Console.WriteLine("n: {0} i: {1}", n, i);
/*debuging*/Console.ReadLine();
}
while (n > 0);
}
}
}

Xem
05-19-2009, 12:28 PM
I figured it out:

The Show/Hide methods:

public void Show(Graphics e)
{
//Pen p = new Pen(Color.Blue, 1.0f);
//e.DrawEllipse(p, this.x-this.r, this.y-this.r, 2*this.r, 2*this.r);
// or
SolidBrush b = new SolidBrush(Color.Blue);
e.FillEllipse(b, this.x-this.r, this.y-this.r, 2*this.r, 2*this.r);
}

public void Hide(Graphics e)
{

//Pen p = new Pen(Color.LightGray, 1.0f);
//e.DrawEllipse(p, this.x-this.r, this.y-this.r, 2*this.r, 2*this.r);
// or
SolidBrush b = new SolidBrush(Color.LightGray);
e.FillEllipse(b, this.x-this.r, this.y-this.r, 2*this.r, 2*this.r);
}

The Form_Event:

Graphics surface = this.CreateGraphics();

C[i].Show(surface);
C[i].Hide(surface);

Anyway, thanx for 0. :(