I have some basic confusions on how to do graphics in C# (and presumably other .Net languages). I'm still fairly new to the .Net event-driven model so I'm a little unclear how I should make everything work together.
I understand that the Paint method draws your form. I believe that the Paint object is invoked anytime the form needs to be refreshed -- like if you just uncovered the window, or something like that.
But what if I've got a happily displayed form, and now I need to change the content dynamically? I'm writing a simple game with a board and pieces, and I want to do some animation on the board when the user makes a move. I've got the board displaying just fine, but I'm not sure how to do the animation.
The Paint method gets called with an object and a PaintEventArgs as params. You use the PaintEventArgs to create a Graphics object and draw using that. (That's what I've figured out so far, anyway. Please feel free to correct any misunderstandings, and if you know of a good source to learn the architecture, I'd love a pointer!!)
So that works for the Paint method. But what about other code that doesn't get passed the PaintEventArgs param? How do I create a Graphics object that can talk to the form to do the animation?
The animation operation will happen when the user clicks on the screen, so maybe I should trigger a graphics operation from the Click event handler?
Thanks,
Gary
Loading
Gary FritzPosted May 24, 2009, 9:59 AM
Gary FritzPosted May 21, 2009, 12:51 PM
namespace Game;
static class Global {
public static BoardClass Game; // I actually use get/put accessors
}
static class Program {
static void main() {
Global.Game = new BoardClass(); // creates and populates gameboard
Application.Run(new BoardGraphics()); // displays board
}
partial class BoardGraphics : Form { // I renamed the default Form1 to BoardGraphics
public BoardGraphics()
{
this.Paint += new PaintEventHandler(DrawBoard);
}
public void DrawBoard(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
// draw the game board using the g Graphics object,
// accessing the BoardClass instance via Global.Game
}
}
public class BoardClass {
// define public data structures for board and board squares
// define logic for the game
}
}
...so among other problems:
* I had to cheat and use a global var to initialize the board from main() so I could also access the board from BoardGraphics (so BoardGraphics knew what to display). Maybe I should define BoardClass methods to retrieve game board info, and use those methods to access the board status from BoardGraphics, but I still need a way to access the BoardClass instance so I can invoke it from other classes. What's the/a right way to do that? Should I define the BoardClass instance as a local in main() and then pass it to anything that needs it? But what if the Paint method needs to call the BoardClass logic -- I can't pass an instance to that Paint event handler?
* The graphics apparently has to be in a separate class -- the partial class created in the Form Designer. That causes some difficulties when the BoardClass (which has all the game logic) has trouble accessing the BoardGraphics class (which has the graphics objects). I suspect I will run into similar problems when I add user interaction (Click events, etc). Again, I suspect I need to reorganize so there's no need for the classes to dig into each other. I'm just not sure how yet.
* Other than the form itself, I'm not sure I used any controls. I just use the Graphics object in the BoardGraphics.DrawBoard method to call DrawLine, DrawPolygon, and DrawImage. Which, since DrawBoard gets invoked by the form Paint method, draws on the form. Which is an instance of BoardGraphics but since it's created by the framework, I'm not even sure where the form instance is. I only know how to access it as "this" in BoardGraphics methods. How do I access it from other classes? Or should I design it so only BoardGraphics methods need to access the form?
* Even though BoardGraphics is the renamed Form1 created by Visual Studio, it doesn't seem to be accepting property changes. I went into Form1.cs [Design] and changed the Text and Size attributes, but it still creates the form with the default size and no text in the title bar.
So... still confused...... :-)
Thanks!!
Gary
Mahesh ChandPosted May 20, 2009, 8:34 PM