Add image to rectangle array - C#

353 views Asked by At

I've got the following code and wanted to know whether there was a way for me to assign the brickImage property to the array featured in my code. If so, how do I achieve this? I basically want to create an array of bricks to be displayed in multiple rows etc.

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

There are 1 answers

11
ymz On

if you want to attach an image to a rectangle simply create your own class:

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

then add this somewhere in your code

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 = ... 
   },
};