Tuesday, March 17, 2009

Creating Tree with delphi

Now, I liked to explain how to do that, but firstly you have to open your Delphi.

Image below is result of code.


The code itself is like here
==================================
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons;

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
DrawGrid1: TDrawGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
kotak: array[0..100,0..100]of integer;
node: array[0..100,0..100]of integer;
cabang: array[0..100,0..100]of integer;
anak: array[0..100,0..100]of integer;
jalan:boolean;
kemungkinan:integer;
implementation

{$R *.dfm}
procedure awal;
var i,j:integer;
begin
jalan:=false;
for i:=0 to 19 do begin
for j:=0 to 19 do begin
kotak[i,j]:=0;
node[i,j]:=random(kemungkinan);
anak[i,j]:=0;
form1.StringGrid1.Cells[j,i]:=inttostr(kotak[j,i]);
cabang[i,j]:=0;
//form1.DrawGrid1.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.Brush.Color:=claqua;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;end;
cabang[9,19]:=1;
node[9,19]:=1;
anak[9,19]:=1;
kotak[9,19]:=1;
form1.DrawGrid1.Canvas.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(9,19));
end;
procedure proses;
var i,j,k:integer;
begin
for i:=1 to 19 do begin
for j:=1 to 19 do begin
if cabang[i,j]=1 then begin
if node[i,j]>0 then begin
if random(kemungkinan)>0 then begin
kotak[i-1,j-1]:=1;cabang[i-1,j-1]:=1;anak[i-1,j-1]:=1;end;
//if random(2)=1 then begin
//kotak[i,j-1]:=1;cabang[i,j-1]:=1;anak[i,j-1]:=1;end;
if random(kemungkinan)>0 then begin
kotak[i+1,j-1]:=1;cabang[i+1,j-1]:=1;anak[i+1,j-1]:=1;end;
end;{node}
cabang[i,j]:=0;
end;{cabang}
if cabang[i,j]=0 then kotak[i,j]:=0;
if anak[i,j]=1 then kotak[i,j]:=1;
form1.StringGrid1.Cells[j,i]:=inttostr(kotak[j,i]);
{warna}
if kotak[i,j]=1 then begin
form1.DrawGrid1.Canvas.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;
if kotak[i,j]=0 then begin
form1.DrawGrid1.Canvas.Brush.Color:=claqua;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;
end;end;{for}
for i:=1 to 19 do begin
for j:=1 to 19 do begin
form1.DrawGrid1.Canvas.Brush.Color:=clwhite;
if kotak[i,j]=1 then begin
for k:=j+1 to 19 do begin
if kotak[i,k]=0 then form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,k));
end;{for}
end;{if}
end;end;{for}
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
awal;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
jalan:=true;
while jalan=true do begin
proses;
application.ProcessMessages;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
jalan:=false;
kemungkinan:=4;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
jalan:=false;
application.Terminate;
end;

end.
========================================
here is another image result, remember to always using randomize or your tree will always has same form in sequence.