Jeg har leget lidt videre med det, det ser ud til at det er tråden der laver en fejl og ikke koden deri. Når jeg instantierer klassen gør jeg det således:
Ball myBall=new Ball(20,20,e.Graphics)
myBall.startBall();
Dette er nødvendigvis ikke den korrekte kode, men således gør jeg det med to linier....
(¯`·._.·[Brian Hvarregaard]·._.·´¯)
Hej Brian,
Din kode ser i og for sig fin ud men den Graphics instans du får fra dit paint event "lever" kun så længe du er inde i eventet.
Jeg har lavet et eks. som bruger din Ball klasse (dog ændret en smule) hvor Ball bliver oprettet i Paint eventet og kørt med det samme. Du kan så trykke på knappen "test" og Ball bliver kørt fra Button_Up eventet men med samme Graphics som kommer fra paint eventet. Du vil se at fejlen kommer så snart du trykker på knappen.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Drawing.Graphics graph;
private System.Windows.Forms.Button button1;
private WindowsApplication1.Ball myBall;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
myBall = null;
graph = null;
this.Paint +=new PaintEventHandler(Form1_Paint);
// myBall = new Ball(10, 10, g);
// myBall.startBall();
// myBall = null;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(376, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 0;
this.button1.Text = "Test";
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(448, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
graph = e.Graphics;
if (myBall == null)
{
Console.WriteLine("Starting ball.");
myBall = new Ball(10, 10, graph);
myBall.startBall();
myBall = null;
}
}
private void button1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (graph != null)
{
Console.WriteLine("Graphics found");
if (myBall == null)
{
Console.WriteLine("Starting ball.");
myBall = new Ball(10, 10, graph);
myBall.startBall();
myBall = null;
}
}
}
}
public class Ball
{
#region Instance Variables
private float x_pos;
private float y_pos;
private float x_speed;
private float y_speed;
private static Graphics mainGraphics;
#endregion
public Ball(float startX, float startY, Graphics g)
{
x_pos=startX;
y_pos=startY;
mainGraphics=g;
}
#region Other methods
public void DrawBall()
{
int times = 500;
Pen myPen=new Pen(Brushes.Black, 2);
while(times-- > 0)
{
Console.WriteLine("x_pos: "+x_pos.ToString()+" y_pos: "+y_pos.ToString());
mainGraphics.DrawEllipse(myPen, x_pos, y_pos, 20, 20);
if ((x_pos += x_speed) > 200)
x_speed = -1;
else
if (x_pos < 10)
x_speed = 1;
if ((y_pos += y_speed) > 50)
y_speed = -1;
else
if (y_pos < 10)
y_speed = 1;
}
}
public void startBall()
{
Thread ballThread = new Thread(new ThreadStart(this.DrawBall));
Console.WriteLine("inde i tråd");
x_speed=1;
y_speed=1;
ballThread.Start();
while (ballThread.IsAlive)
Thread.Sleep(0);
ballThread.Join();
}
#endregion
#region Properties
public float XPosition
{
get { return x_pos; }
set { x_pos=value; }
}
public float YPosition
{
get { return y_pos; }
set { y_pos=value; }
}
#endregion
}
}
En mulighed kunne måske være at oprette en forkomst af Graphics i din Ball og der fra sende et event til din form som så tegner din Ball.Graphics på den aktuelle Graphics i Paint eventet (e.Graphics).
/Michael.