Collision
-
Hello, You have to be carefull with the GDI objects here (memory leak)! If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class. Same to Rectangles. But you have to dispose (free the resourcess) them in the dispose method of your class (Form).
System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty; System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty; System.Drawing.Pen penBlack = new Pen(Color.Black, 3); System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need! private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } if(penBlack !=null) penBlack.Dispose(); if(penRed !=null) penRed.Dispose(); } base.Dispose( disposing ); }
If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly.private void Form1_Paint(object sender, PaintEventArgs e) { Graphics actGraphics = e.Graphics; using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3)) using(System.Drawing.Pen penRed = new Pen(Color.Red, 3)) { rectangle1 = new System Drawing.Rectangle(15, 15, 200, 150); rectangle2 = new System Drawing.Rectangle(35, 35, 200, 150); actGraphics.DrawRectangle(penBlack , rectangle1); actGraphics.DrawRectangle(penRed , rectangle2); } }
Hope it helps!All the best, Martin
That just helped a lot, and there's SIMPLE collision detection! All I fond was 50 lines of code that was... extremely confusing and hard, but thatnks again to everyone who replied... Time to continue reading.