Living Lazarus
Monday, October 09, 2006
 
RandomRange function

Delphi has a great function that Lazarus lacks; the ability to return a random number within a set range. However, this little function will do the job really well.

Declare the following in the public section,

function RandomRange(aMin: Integer; aMax: Integer) : Integer;

Now add this to your implementation section

function TForm1.RandomRange(aMin: Integer; aMax: Integer) : Integer;
begin
RandomRange := Random(aMax-aMin) + aMin ;
end;


Usage is very simple,

declare a variable, say aNum and then use like this,

aNum := RandomRange(10,100);

Simple.

 
Friday, October 06, 2006
 
Control Arrays

Lazarus does not have the inbuilt facility to create control arrays so a work around must be used. This is not as difficult as it might seem. Try the following.

Create and save a new project called Experiment.

Place an Image onto the form you have created. Name it shapeImage_0. Set its Visible property to false.

Now, in the Public declarations section create an array so...

images : array[1..20,1..20] of TImage;

Underneath create a procedure called Shapes(i : integer; f : integer);

In the var section put this.. MAP : array[1..20,1..20] of integer;

Now create the Shapes procedure..

procedure TForm1.Shapes(i : integer; f : integer);
begin
with images[i,f] do
begin
Canvas.Rectangle(20,20,0,0);
Case MAP[i,f] of
0 : begin Canvas.Brush.Color := clYellow;
Canvas.Pen.Color := clYellow;
Canvas.Ellipse(5,5,15,15);
end;
1 : begin Canvas.Brush.Color := clWhite;
Canvas.Pen.Color := clWhite;
Canvas.Ellipse(5,5,15,15);
end;
2 : begin Canvas.Brush.Color := clRed;
Canvas.Pen.Color := clRed;
Canvas.Ellipse(1,1,18,18);
end;
3 : begin Canvas.Brush.Color := clBlue;
Canvas.Pen.Color := clBlue;
Canvas.Ellipse(1,1,18,18);
end;
end;
end;

end;


Now, fill in the FormCreate like this..

procedure TForm1.FormCreate(Sender: TObject);
var
i, f, g, nCtrl : integer;
begin

{ create images for shapes }
nCtrl := 0;
for f := 1 to 20 do
begin
for i := 1 to 20 do
begin
Inc(nCtrl);
images[f,i] := TImage.Create(self);
with images[f,i] do
begin
parent := self;
tag := nCtrl;
name := 'shapeImage_' + IntToStr(tag);
visible := True;
autosize := False;
stretch := false;
Height := 20;
Width := 20;
Left := 10 + (i*20) - 19;
Top := 5 + (f*20) -20;
Canvas.Pen.Color := clBlack;
Canvas.Brush.Color := clBlack;
OnClick := @shapeImage_0Click;
end;
end;
end;

Randomize;
for i := 1 to 20 do
begin
for f := 1 to 20 do
begin
g := Random(100);
if(g >= 1) then MAP[i,f] := 0;
if(g >= 30) then MAP[i,f] := 1;
if(g >= 60) then MAP[i,f] := 2;
if(g >= 80) then MAP[i,f] := 3;
Shapes(i, f);
end;
end;
end;



Now, to end create an OnClick event with the TImage you placed on the form and fill it with this..


procedure TForm1.shapeImage_0Click(Sender: TObject);
var
j, k, l, m: integer;
i : Longint;
begin
i := TImage(sender).Tag;
for j := 1 to 20 do
begin
for k := 1 to 20 do
begin
if images[j,k].Tag = i then
begin
l := j;
m := k;
end;
end;

end;

MessageDlg( 'You have clicked on ' + IntToStr(l) + ',' + IntToStr(m), mtInformation, [mbOk],0);
end;

Save and execute the application and you should see this..
















Click on one of the shapes and a message dialog will pop up and tell you which element of the control array you accessed.

That's it, from this starting point you can do a great deal. I'm sure you can think of more!
 
One mans exploration into the possibilities of an open source RAD

Name:
Location: Townshend, Hayle, Cornwall, United Kingdom
ARCHIVES
September 2006 / October 2006 / November 2006 / April 2007 / July 2007 / November 2007 / May 2008 / September 2009 / March 2011 / January 2013 /


Powered by Blogger