how to do very simple animation?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What I want to do is a stretch to even call it animation. I'm just trying to
display moving a piece on a board game. Basically, erase the old position of
the piece, and re-draw it a few spaces over.

I've bean searching through and reading various MSDN articles, like 'Gaming
with the .NET Compact Framework' and it's way beyond what I need to do. I
have a form with a bitmap for the board and a couple other buttons and
labels, and I simply want to draw the player's pieces on top and then move
them. From what I've read in these articles, I'm not sure if I need to
override the onPaint() method (which I've tried and it didn't work), or do
some drawing independently in my own methods (which I've also tried and it
didn't work).

Here's what I've tried, this is a method that is called from the Constructor
of my form, after the InitializeComponent() :

private void drawPieces()
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(System.Drawing.Color.Crimson);
g.DrawEllipse(pen, 50, 50, 15, 15);
}

And nothing appears. I've tried basically the same code in the overrided
OnPaint method, but using the Graphic object from the PaintEventArgs.
Neither one of these drew the circle.

What am I doing wrong? Any help would be much appreciated. Thanks.
 
Animation could be embedded as GIF file and you may use AnimationControl
from ("Creating a Microsoft .NET Compact Framework-based Animation
Control"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/animationcontrol.asp)
to display this GIF file.

By the way, to make your changes visible instead of this.CreateGraphics
try to put your code snippet into the OnPaint event of your form:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(System.Drawing.Color.Crimson);
g.DrawEllipse(pen, 50, 50, 15, 15);
}

Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
I appreciate your response, but wonder if you read my post. I'll admit the
subject title was a bit misleading but I thought I clarified that in the
first sentence of the original post. I'm not talking about an animated gif.
I'm trying to simply draw on a form that already has a bitmap and buttons on
it. I'm trying to draw the piece (a simple circle) on top of the bitmap.
Then, when the user clicks movement buttons, move the piece, by erasing the
former position of the piece and drawing it one space over.
 
Actually, GIF animation was suggested by me just as an alternative
approach for your animation. It's your choice to use it or not... To
implement what you want just use second bitmap that will be created in
memory and when you finish your drawing, just put this buffer to the
window in the OnPaint event handler:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Bitmap buffer = new Bitmap(ClientSize.Width, ClientSize.Height);
using (Graphics g = Graphics.FromImage(buffer))
{
g.DrawImage(yourImage, 0, 0);

Pen pen = new Pen(System.Drawing.Color.Crimson);
g.DrawEllipse(pen, 50, 50, 15, 15);
}

g.DrawImage(buffer, 0, 0);
buffer.Dispose();
}



Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
It depends on what you want. :) If you want the fast speed then
Pen.Dispose you may not call (Pen.Dispose does nothing for CF.NET, as
well as Brush.Dispose). If you want write "correct code", I mean call
dispose everytime and everywhere as a best practice then I've missed
something...

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Yes I am of the pedantic kind where "thou shall call dispose if it is there"
:-)
The empty dispose is surely going to be inlined so no perf hit, except when
you are typing it :-)

Cheers
Daniel
 
Back
Top