将图像添加到矩形数组 - C#

时间:2014-11-25 10:15:46

标签: c# class multidimensional-array rectangles drawimage

我已经获得了以下代码,并想知道我是否有办法将brickImage属性分配给我的代码中的数组。如果是这样,我该如何实现?我基本上想要创建一个砖块数组,以多行显示等。

public class Brick
    {
        private int x, y, width, height;
        private Image brickImage;
        private Rectangle brickRec;
        private Rectangle[] brickRecs;

        public Rectangle BrickRec
        {
            get { return brickRec; }
        }


        public Brick()
        {
            x = 0;
            y = 0;
            width = 60;
            height = 20;


            brickImage = Breakout.Properties.Resources.brick_fw;

            brickRec = new Rectangle(x, y, width, height);

            Rectangle[] brickRecs =
            {
                new Rectangle(0, 0, 60, 20),
                new Rectangle(0, 0, 121, 20),
                new Rectangle(0, 0, 242, 20)

            };

        }


        public void drawBrick(Graphics paper)
        {
            paper.DrawImage(brickImage, brickRec);

            //paper.DrawImage(brickImage, brickRecs);
        }

    }

1 个答案:

答案 0 :(得分:0)

如果您想将图片附加到矩形,只需创建自己的类

public class MyBrick
{
   public Image Image {get; set;}
   public Point[] Locations {get; set;}
}

然后在代码中的某处添加

MyBrick[] brickRecs  = 
{
   new MyBrick()
   {
       Locations = 
       {
          new Point(60, 20),
          new Point(10, 10),
          //....
       },
       Image = ... // ADD IMAGE REFERENCE HERE
   },

   new MyBrick()
   {
       Locations = 
       {
          new Point(90, 90),
          new Point(5, 5),
          //....
       },
       Image = ... 
   },
};