コード例 #1
0
        private void DrawField(Game game)
        {
            int rows           = game.Rows;
            int columns        = game.Columns;
            int width          = (int)Math.Round((double)pictureBox1.Width / columns);
            int height         = (int)Math.Round((double)pictureBox1.Height / rows);
            var aliveBrush     = new SolidBrush(Color.Green);
            var deadBrush      = new SolidBrush(Color.Black);
            var bitmap         = new Bitmap(pictureBox1.Image);
            var bitmapGraphics = Graphics.FromImage(bitmap);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (game.HasCellChangedState(i, j))
                    {
                        bitmapGraphics.FillRectangle(
                            game.GetCellState(i, j) == CellState.Alive ? aliveBrush : deadBrush,
                            j * width,
                            i * height,
                            width,
                            height);
                    }
                }
            }
            pictureBox1.Image = bitmap;
            aliveBrush.Dispose();
            deadBrush.Dispose();
            bitmapGraphics.Dispose();
        }