Click here to Skip to main content
15,895,011 members

Array of Rectangles declaration problem

ssiidd asked:

Open original thread
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++;
               }
           }
Tags: C#, Graphics

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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