动态生成m(列)棋盘的n(行)

时间:2011-10-20 14:54:01

标签: c# silverlight-4.0

想象一下以下场景(Silverlight 4)。我有两个矩形。一个是黑色,一个是白色。它们都有固定的尺寸,比如50x50。我还有一个区域(500x500),我想用这些矩形填充但是以交替的方式 - 白色,黑色,白色,黑色等。为了更好地说明,请看下面的链接:

http://screencast.com/t/BwsPSbtg2eaM

http://screencast.com/t/gTuexSSyW

视频(链接#2)正好展示了我想要实现的目标。

非常感谢任何帮助!

琼斯

1 个答案:

答案 0 :(得分:1)

    int totalRectsInaRow = TotalWidth/ WidthOfOneRect;
    int totalRectsInaColumn = TotalHeight/ HeightOfOneRect;

    //Create a Grid of Width = TotalWidth and Height = Total Height;
    //Add columns equal to totalRectsInaColumn and rows equal to totalRectsInaRow in Grid
    //Set wdith of each column equal to width of one rectangle
    //set height of each row equal to height of one rectangle

    bool drawWhite = true;
    for (int i = 0; i < totalRectsInaColumn; i++)
    {
        for (int j = 0; j < totalRectsInaRow; j++)
        {
            if (drawWhite)
            { 
                //draw white rectanlge at i column and j row
                //basically you create a rectangle and place it in grid on particular location
                DrawWhileRectangle(i, j);
                drawWhite = false;
            }
            else
            {
                //draw black rectanlge at i column and j row
                //basically you create a rectangle and place it in grid on particular location
                DrawBlackRectangle(i, j);
                drawWhite = true;
            }
        }
        drawWhite = !drawWhite;
    }