tags:

views:

70

answers:

1

I've worked out most of the code and have several game classes. The one bit I'm stuck on at the moment, it how to draw the actual Connect 4 grid. Can anyone tell me what's wrong with this for loop? I get no errors but the grid doesn't appear. I'm using C#.

private void Drawgrid() 
{
    Brush b = Brushes.Black;
    Pen p = Pens.Black;
    for (int xCoor = XStart, col = 0; xCoor < XStart + ColMax * DiscSpace; xCoor += DiscSpace, col++)
    {
        // x coordinate beginning; while the x coordinate is smaller than the max column size, times it by
        // the space between each disc and then add the x coord to the disc space in order to create a new circle.
        for (int yCoor = YStart, row = RowMax - 1; yCoor < YStart + RowMax * DiscScale; yCoor += DiscScale, row--)
        {
            switch (Grid.State[row, col])
            {
                case GameGrid.Gridvalues.Red:
                    b = Brushes.Red;
                    break;
                case GameGrid.Gridvalues.Yellow:
                    b = Brushes.Yellow;
                    break;
                case GameGrid.Gridvalues.None:
                    b = Brushes.Aqua;
                    break;
            }
        }
        MainDisplay.DrawEllipse(p, xCoor, yCoor, 50, 50);
        MainDisplay.FillEllipse(b, xCoor, yCoor, 50, 50);
    }
    Invalidate();
}
+2  A: 

The code in Drawgrid() needs to be executed when the window is redrawing itself.

The Invalidate() call tells the application that it needs to redraw the window contents (it triggers a redraw of your window). This code (with the exception of the Invalidate() call) should be in your overridden OnPaint() method, otherwise whatever gets drawn by this code will be immediately overwritten by the default drawing code in OnPaint() (which, by default, will probably draw a white background) when you make the Invalidate() call.

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    // (your painting code here...)
}
Scott Smith
Sorry, I don't have an onDraw() method.Before this function I have this code to store the image as a bitmap: private void Form1_Load(object sender, EventArgs e) { MainBitmap = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); MainDisplay = Graphics.FromImage(MainBitmap); MainDisplay.Clear(Color.Aqua); Drawgrid(); }Thank you for the quick reply.
Matt Wilde
Sorry that it isn't nicely spaced out in the above comment.
Matt Wilde
@Matt Wilde: That is not how you do custom drawing in Winforms. You need to do what Scott says, inherit your board control from `System.Windows.Forms.Control` and override its `OnPaint` method. If you want to use a bitmap as some kind of buffer, that is OK (not great), but then you actually need to draw the bitmap to the control surface in the `OnPaint` method using `Graphics.DrawImage`. Simply drawing on an in-memory bitmap does not make its content magically appear on screen.
Aaronaught
I'm sorry, I'm trying to work out what you mean (I'm very new to C# and have just started a module in 'Software Development' at University).I call Drawgrid() at the end of the Form1_Load(object sender, EventArgs e), is this not enough?
Matt Wilde
@Matt - In a windowing system, a window needs to be able to redraw its contents at various times. Rather than having a one-time method that draws on the window, the drawing code is located in a callback (typically called OnDraw or OnPaint). The Operating system will call this method whenever the window's appearance needs to be refreshed. Your code must be within such a callback - this is how you do drawing in a window. Look up **OnPaint** in C# literature.
Scott Smith
Thanks for all your help. Problem solved. Phew.
Matt Wilde