Linking array and drawgrid

Can anyone tell me how to create an array of 10,10 integers in a separate class.  Fill this array with zeroes (0) and ones (1) alternately.  Putting a drawgrid on my form and have depending on the value in my array (in separate class) a circle with different colour (drawcell) in the cell of the drawgrid.

Thank you.
pmelkebekeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
denizcanConnect With a Mentor Commented:
There is two way of doing it...

One is using a array to mask...
 MyMask: array[1..10, 1..10] of Boolean;

  initiallly all values are False. To do this:
 
FillChar(MyMask, SizeOf(MyMask), 0);

  than you should set the value of MyMask[ACol, ARow] True when OnMouseClick accours; And Invalidate the DrawGrid to force it repaint.

Now in the DrawCell method, change this line:
  if (ACol > 0) and (ARow > 0) then
to
  if (ACol > 0) and (ARow > 0) and MyMask[ACol, ARow] then
....

The second way is to use a buffer array:

MyBuffer: array[1..10, 1..10] of Integer;

FillChar(MyBuffer, SizeOf(MyBuffer),$FF);

You should set the value of MyBuffer[ACol, ARow] to the original value MyArray[ACol, ARow], then Invalidate the grid..

That's all...

0
 
pmelkebekeAuthor Commented:
I'm a newbie, please be understanding with my problem.
0
 
scrapdogCommented:
You necessarily need to define a class for the array?  Or can you use it as a global variable, or as a member of your Form class?

To declare an array:

var
  MyArray: array[1..10, 1..10] of integer;


To fill the array:

var x :boolean;  i,j :integer;

x := false;
i := 1;
while (i<=10) do begin
  j := 1;
  while (j <=10) do begin
    MyArray[i,j] := ord(x);
    x := not x;
    j := j + 1;
  end;
  i := i + 1;
end;

To do it in a separate class is not something a newbie should try to conquer...




0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
pmelkebekeAuthor Commented:
Ok the array is filled now, Thanks for that.  But ho do I link this to my drawgrid so I can view different colors depending on the values in the array.  Let's say if it's a 0 then make it a blue circle, if it's a 1 make it a red circle.
0
 
pmelkebekeAuthor Commented:
To be correct the array makes a part of a class.
0
 
denizcanCommented:
for I := 1 to 10 do
  for J := 1 to 10 do
    MyArray[I, J] := (I + J) and 1;

seems smarter...

pmelkebeke, do you want to create a component that screens this array, or just want to show that array...

The former needs complicated steps, but letter is easy...

for letter:
I think you know how DrawGrids works. You should use it's DrawCell event to draw something. Double click, OnDrawCell event, and write a code stg like that:

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (ACol > 0) and (ARow > 0) then
    with TDrawGrid(Sender) do
    begin
      case MyArray[ACol, ARow] of
        0: Canvas.Brush.Color := clRed;
        1: Canvas.Brush.Color := clBlue;
        ...
        else Canvas.Brush.Color := MyDefaultColor;
      end;
      Canvas.Ellipse(Rect.Left, Rect.Top, Rect.Left + 10, Rect.Top + 10);
    end;
end;

Here you can fill the array not only 0 and 1's, but also other numbers, and set the color of brush with these numbers...
0
 
pmelkebekeAuthor Commented:
So far so good.  Now the following is what I really want:

I have a main form (frmMain) with a drawgrid on it.  I have a unit with my classes in there (uClasses).  In this unit I have a class (Bord) that has an array in it of integers (10,10).  On initialization (Bord.Init) of the Bord-class the array needs to be filled with the zeroes and ones.  On clicking a button on the form the Drawgrid needs to be filled with the colored dots looking at the values of the array in my Bord-class to determine the color.  If 0 then red, if 1 then blue, anything else should be blank.

0
 
pmelkebekeAuthor Commented:
Thanks alot you've been a great help!
0
 
denizcanCommented:
not at all...
take care...
0
All Courses

From novice to tech pro — start learning today.