2010-03-11 33 views
2

我已经制定了大部分代码并且有几个游戏类。我现在坚持的一点是,它如何绘制实际的Connect 4网格。谁能告诉我这个for循环有什么问题?我没有得到任何错误,但网格并未出现。我正在使用C#。连接4 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(); 
} 
+0

@John Rasch - 对不起,我们都似乎在编辑。我会停下来。 – 2010-03-11 18:41:05

回答

2

当窗口重绘本身时,需要执行Drawgrid()中的代码。

Invalidate()调用告诉应用程序它需要重新绘制窗口内容(它会触发重绘窗口)。此代码(除Invalidate()调用外)应该在您的覆盖OnPaint()方法中,否则无论由此代码绘制什么,都将立即被OnPaint()中的默认绘图代码覆盖(默认情况下,它可能会绘制白色背景)当你拨打Invalidate()时。

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

    // (your painting code here...) 
} 
+0

对不起,我没有一个onDraw()方法。 这个功能我有这样的代码,以将图像存储为位图之前: 私人无效Form1_Load的(对象发件人,EventArgs的) { MainBitmap =新位图(this.ClientRectangle.Width,this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); MainDisplay = Graphics.FromImage(MainBitmap); MainDisplay.Clear(Color.Aqua); Drawgrid(); } 感谢您的快速回复。 – 2010-03-11 18:36:29

+0

对不起,在上面的评论中没有很好地分开。 – 2010-03-11 18:37:39

+0

@Matt Wilde:这不是你如何在Winforms中进行自定义绘图。你需要做Scott所说的,从'System.Windows.Forms.Control'继承你的板子控制并覆盖它的'OnPaint'方法。如果你想使用一个位图作为某种缓冲区,那么可以(不是很好),但是实际上你需要使用'Graphics.DrawImage'在'OnPaint'方法中将位图绘制到控制表面。简单地在内存位图上绘图不会使其内容奇迹般地出现在屏幕上。 – Aaronaught 2010-03-11 18:43:45