Click here to Skip to main content
13,002,000 members (78,683 online)
Rate this:
Please Sign up or sign in to vote.
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;

rect[(m*m)]=new Rectangle();
                   rect [k]=new Rectangle((x+(j*offset)),(y+i*offset)),offset,offset);
Posted 6-Mar-12 3:22am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:
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:
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:
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.
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! :)
Pete O'Hanlon 6-Mar-12 14:46pm
You're welcome. I'm glad I could help you out.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 6 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100