|
All drawing should occur in your paint event. The reason is, Windows tells your form when to draw itself, for example if it was obscured by another window and then shown again. That's what the Paint event is for.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Yeah, thanks, I just found this good article that explains it well... In the paint event, you set up graphics, ad then (whatever your variable is).DrawWhatever (blah, blah, blah)
Thanks again!
|
|
|
|
|
Exactly. The other piece of the puzzle is, if you change something and want to force a redraw, you call Invalidate() to force a paint event.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
Can I suggest that you look at abstracting your drawing so that you don't end up with a huge paint routine? For instance, you want to draw a rectangle - then why not create a class that is responsible for managing the rectangle? The reason that I suggest this, is that it makes it easier to manage the drawing process if the components themselves are responsible for drawing themselves.
Typically, you would have a shape that inherits from some common class (call it ShapeBase for instance). Then you would add the shapes to a shape collection. When you call paint in your form, you would simply need to cycle through each shape in the collection and tell them to paint themself.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I've been reading this Dummies book for starters, (that's to the same people, they KNOW who they are...), but anyways, I'm trying to figure out some simple kind of collision detection for 2 squares, not per pixel. I've got some to work, but I need to know the correct way to do this, so if anyone would teach me a little on collision, I'd be very happy. Thanks in advance.
|
|
|
|
|
Collision of squares is easy. I believe the Rectangle class has a PtInRect method, just pass that the four corners of your other rectangle and see if any of them return true. A rectangle is easy because if any of the corners are inside the other rectange, then they have collided. Irregular shapes are more complex, I present one solution ( although I don't believe it's overly efficient ) in my series of articles on writing a game in C#.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I see what you are getting at, but could you give me a look at the code, just a simple snippet?
|
|
|
|
|
It's even easier than I thought:
Rectangle rc1 = new Rectangle(0, 0, 20, 20);
Rectangle rc2 = new Rectangle(19, 21, 5, 5);
if (rc2.IntersectsWith(rc1))
{
// match
}
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Are you serious?
|
|
|
|
|
Wait, but when I run the program, you can't see the rectangles. It's probably supposed to do that, but how do you use both collision and drawing? I get them seperate, but... Thnks for the help so far though.
|
|
|
|
|
Please see my response to your other post to get an idea on how to do this.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Like I said, Invalidate() forces a paint. And what Pete said, I was gong to say, too. You should have a collection of objects that know how to draw themselves. Your paint event should step over that collection, and pass the drawing to them. This lets you easily add as many rectangles as you like, and your paint event stays clean, no matter how many shapes you add in the future.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I only said it because you are busy racking up about 1000 posts a day now (at least that's how it seems to us mortals).
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
LOL - either way, I thought to say it and didn't, so it was a fair addition to what I posted, I should have mentioned it.
It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code.
BTW, Nish pointed out to me that I've gone over the 40,000 post mark. That's got to be a sign that I need to get a life.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Christian Graus wrote: It's nice to see MasterSharp both taking my advice to read some books, and starting to ask more complex questions ( which I assume means he's learning something from the books he's reading ). I feel pretty happy to think that someone actually came here wanting to learn and not just to be given code.
I know - it's always nice to see them grow up. BTW - I haven't forgotten your CD, it's just that we're stuck in the middle of a damn postal strike here in the UK. I don't want it to "get lost" in the post.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
OK, cool. LMK when that resolves itself.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
What are you talking about 100 posts a day. I know I'm VERY questioning, but not that many even for exagerations (probably killed that), and I've calmed down a bit and took to a book!
|
|
|
|
|
MasterSharp wrote: What are you talking about 100 posts a day
Not you - Chris. He's the most prolific poster on CodeProject.
MasterSharp wrote: I've calmed down a bit and took to a book!
That's the spirit. I buy (and read) 3 or 4 computer texts every other month. Hopefully that helps to keep me sharp.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
FYI - If you are interested in theory with a little code - mind you it is C++ but that's not important, check out the book "Beginning Math and Physics for Game Programmers" by Wendy Stahler. It is pretty good and might help you with game development. ISBN is 0-7357-1390-1. Anyhow, I found it to be invaluable in my game/physics programming.
Good Luck!
“You can't teach people to be lazy - either they have it, or they don't.”
-Dagwood Bumstead
|
|
|
|
|
|
Okay, I have just one more question about the this whole mess. After you assign the graphics...
private void Form1_Paint(object sender, PaintEventArgs e)<br />
{<br />
Graphics G = e.Graphics;<br />
<br />
G.DrawRectangle(new Pen(Color.Black, 3), 15, 15, 200, 150);<br />
<br />
G.DrawRectangle(new Pen(Color.Red, 3), 15, 15, 200, 150);<br />
(for example)
... how do you name those rectangles?
|
|
|
|
|
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);
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
|
|
|
|
|
Hey thanks, that's just what I needed!
|
|
|
|
|