Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2009, 04:23 PM   PM User | #1
Xem
New to the CF scene

 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Xem is an unknown quantity at this point
Circle animation

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.

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

Thanx!!!

Listing (yes, written for a console output ):

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 is offline   Reply With Quote
Old 05-19-2009, 12:28 PM   PM User | #2
Xem
New to the CF scene

 
Join Date: Mar 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Xem is an unknown quantity at this point
Post

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.

Last edited by Xem; 05-19-2009 at 12:32 PM..
Xem is offline   Reply With Quote
Reply

Bookmarks

Tags
circle, draw, graphics

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.