oto metody potrzebne do zanimowania piłki:
Bitmap animatedImage = new Bitmap("C:\\Documents and Settings\\Tony\\Pulpit\\t\\z.gif");
bool currentlyAnimating = false;
public void animate()
{
if (!currentlyAnimating)
{
//Begin the animation only once.
ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
currentlyAnimating = true;
}
}
private void OnFrameChanged(object o, EventArgs e)
{
//Force a call to the Paint event handler.
this.Invalidate();
}
public static void SetDoubleBuffered(Control control)
{
// set instance non-public property with name "DoubleBuffered" to true
typeof(Control).InvokeMember("DoubleBuffered",
System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
null, control, new object[] { true });
}
protected override void OnPaint(PaintEventArgs e)
{
animate(); //Begin the animation
ImageAnimator.UpdateFrames(); //Get the next frame ready for rendering.s
e.Graphics.DrawImage(this.animatedImage, new Point(X, X)); //Draw the next frame in the animation.
SetDoubleBuffered(this);
}
w konstruktorze Form1 tworzę jej obiekt klasy Graphics oraz obiekt mojej klasy, i do jej konstruktora wrzucam ten graficzny obiekt
public partial class Form1 : Form
{
Soccer soccer;
Graphics s;
int X = 50;
public Form1()
{
InitializeComponent();
s = this.CreateGraphics();
soccer = new Soccer(s);
}
[...]
}
konstruktor klasy Soccer dokonuje m.in. rysowania boiska:
Pen p = new Pen(Color.Yellow, 2);
//punkty na boisku
for (int i = 2; i <= 24; i++)
{
for (int j = 1; j < 16; j++)
{
grafika.DrawEllipse(p, j * 18 - 1, i * 18 - 1, 1, 1);
}
}
p.Color = Color.White;
p.Width = 3;
//gorne linie
grafika.DrawLine(p, new Point(18, 35), new Point(108, 35));
grafika.DrawLine(p, new Point(180, 35), new Point(270, 35));
//srodkowa linia
grafika.DrawLine(p, new Point(18, 234), new Point(270, 234));
//dolne linie
grafika.DrawLine(p, new Point(18, 432), new Point(108, 432));
grafika.DrawLine(p, new Point(180, 432), new Point(270, 432));
//linie boczne
grafika.DrawLine(p, new Point(18, 35), new Point(18, 434));
grafika.DrawLine(p, new Point(270, 35), new Point(270, 434));
//bramki
p.Color = Color.Red;
grafika.DrawLine(p, new Point(108, 35), new Point(108, 17));
grafika.DrawLine(p, new Point(108, 17), new Point(180, 17));
grafika.DrawLine(p, new Point(180, 17), new Point(180, 35));
p.Color = Color.Green;
grafika.DrawLine(p, new Point(108, 432), new Point(108, 450));
grafika.DrawLine(p, new Point(108, 450), new Point(180, 450));
grafika.DrawLine(p, new Point(180, 450), new Point(180, 432));
i tutaj, na samym końcu będzie animacja piłki (znajduje się ona w OnPaint). Nie bardzo wiem jak to rozwiązac. Mam nadzieję że powyższy opis będzie pomocny :)