Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,
Im trying to implement a tiling algorithm in C#. I'm having a couple of issues,
my approach is to take the size of a grid m x m as input, and then draw the grid using DrawRectangles method on my graphics object. the user then selects a tile around which the grid gets tiled.
My issues:
one : the OnPaint eventhandler only shows the graphics i want to generate when called on the form. I tried calling it on a picturebox control and a panel control but the graphics dont show up?
two: im trying to put my rectangles in an array so i can identify which rectangle is selected and apply a tiling algo based on which tile(rectangle) is selected. But its not working. NullExceptionReference is what is says when i run the code. Im an amateur, so please let me know if i'm declaring it incorrectly? or Am i using the wrong data structure?

public Rectangle [] rect;

C#
rect[(m*m)]=new Rectangle();
           k=1;
           for(i=0;i<m;i++)
           {
               for(j=0;j<m;j++)
               {
                   rect [k]=new Rectangle((x+(j*offset)),(y+i*offset)),offset,offset);
                   g.DrawRectangle(myPen,rect[k]);
                   
                   k++;
               }
           }
Posted

1 solution

As you are instantiating a rectangle and then drawing it immediately, there doesn't seem any reason for your code to be actually storing an array of rectangles. The simple way for you to draw the rectangle there is to simply replace the inner part of your loop with:
C#
g.DrawRectangle(myPen, new Rectangle(x+(j*offset), (y+i*offset), offset, offset));
Is there processing elsewhere that requires this list of rectangles, or is it sufficient that you are simply drawing it. BTW, by doing this you can get rid of the rectangle array declaration altogether.

[Edit]Following clarification from the OP as to what he was after:

I would separate the creation of the rectangle away from the drawing of the rectangle. One problem you currently face is that you are instantiating and populating the array in every OnPaint event. Instead, create it prior to drawing like this:
C#
private List<Rectangle> rectangles = new List<Rectangle();
private void SetupRectangles(int m, int offset)
{
  for (int i = 0; i < m; i++)
  {
    for (int j = 0; j < m; j++)
    {
      rectangles.Add(new Rectangle(x+(j*offset), (y+i*offset), offset, offset));
    }
  }
}
Then in your OnPaint handler, all you need do is this:
C#
if (rectangles.Count == 0)
{
  // This will only happen once. This could be moved elsewhere, but I don't know   
  // where you are setting up offset and m.
  SetupRectangles(m, offset); 
}
foreach (Rectangle rect in rectangles)
{
  g.DrawRectangle(myPen, rect);
}
It's as simple as that.
 
Share this answer
 
v2
Comments
ssiidd 6-Mar-12 10:52am    
Yes, after these rectangles are drawn, the user is supposed to click one of them. I need to be able to record which one was selected..and then run the tiling algorithm on it, i.e., find out which quadrant of the grid the selecvted tile lies on and then surround it by trominos (L shaped tiles)
Pete O'Hanlon 6-Mar-12 11:04am    
I have updated my answer. This might help you.
ssiidd 6-Mar-12 13:32pm    
Thank you for your solution. :) but ive been trying to find any onmouseClick event tutorials to handle the clicking of the rectangle so i can iterate through the list to find which one was clicked? i cant seem to find any! any direction would be helpful.
Pete O'Hanlon 6-Mar-12 13:49pm    
When you have added your mouse event handler, all you need do (inside the event handling code), is get the point that the mouse click occurred and iterate over the rectangles, calling Rectangle.Contains with the point. The mouse click handily gives you the Location of the mouse click which you can use in your call to the Contains method.
ssiidd 6-Mar-12 14:36pm    
Thank you so much! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900