Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Changing a simulation component into an Image on a Form!

basmahbasmah Posts: 1Member
Hi everyone,

Initially i was using a component to run this simulation. However, now i would like to get rid of the component and run the simulation on the form using an Image. I've managed to transfer everything into a TImage called TImage1 and list all its properties and variables to be used later in the logic. However, the simulation wouldn't run and it keep coming up with an Access violation error message everytime i try 2 start the simulation by clicking the button on the form.

I've got 2 Units: 1) ProcessUnit unit where all the logic and the TImage1 code resides and 2) MainUit which is the Form Unit which I have a Button and 2 Images.

Any idea where I am going wrong, please reply asap. Thank you for your time and co-operation.

================================================================
unit ProcessUnit;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls;

type
ChickenRecord = record
ChickenX, ChickenY : Integer;
ChickenType : Integer;
Dir : Integer;
PrevC : TColor;
PrevCount : Integer;
end;

TImage1 = class(TImage)
private
SqX : Integer ; { No. Of Squares X }
SqY : Integer ; { No. of Squares Y }
NoChicks : Integer;
SizeX,SizeY : Integer;
Chickens : Array[0..100] of ChickenRecord;
Buffer : TBitMap;
ATimer : TTimer;
TimerEnable : Boolean;
AInterval : Integer;

Procedure MakeGrid; { Creates the Grid}
Procedure ColorGrid; { Colours the grid in}
procedure SetX(Value : Integer);
Procedure SetY(Value : Integer);
procedure TimerE(Value : Boolean);
procedure TickerSet(Value : Integer);
function GetColor(X : Integer ; Y : Integer) : TColor;
function CheckCollision(X : Integer; Y: Integer) : Boolean;

protected
procedure Paint; override;
procedure MoveChickens(Sender:TObject);
procedure SetChicks(Value : Integer);
Procedure FillGrid(StartX : Integer ; StartY : Integer ; Color : TColor);

public
constructor Create; //(AOwner: TComponent); override;
destructor Destroy; override;

published
property DimesionsX : Integer read SqX Write SetX;
property DimesionsY : Integer read SqY Write SetY;
property ChickensEnabled : boolean read TimerEnable write TimerE;
property MoveInterval : integer read AInterval write TickerSet;
property NoOfChicks : Integer Read NoChicks Write SetChicks;
property OnClick;
end;

implementation

constructor TImage1.Create; //(AOwner : TComponent);
begin
// inherited Create(AOwner);
Width := 400;
Height := 200;
SqX:= 50;
SqY:= 50;
Buffer:=TBitmap.Create;
ATimer:=TTimer.Create(Self);
ATimer.Enabled:=False;
ATimer.Interval:=1000;
end;

procedure TImage1.TimerE(Value : Boolean);
begin
ATimer.Enabled:=Value;
TimerEnable:=Value;
end;

function TImage1.GetColor(X : Integer ; Y : Integer) : TColor;
var
TempX : Integer;
TempY : Integer;
Begin
TempX := X * SizeX;
TempY := Y * SizeY;
Result:=Buffer.Canvas.Pixels[TempX, TempY];
End;

function TImage1.CheckCollision(X : Integer; Y: Integer) : Boolean;
Var
A :Integer;
Collides: Boolean;
Begin
A:=1;
Collides:=False;

While (A NoChicks then
begin
If Value > 100 then
begin
Showmessage('100 Chickens Maximum');
end
else
begin
NoChicks:=A;
Randomize;
{ Randomize Chickens }
for A:=1 to NoChicks do
begin
Chickens[A].ChickenX:=random(SqX+1);
Chickens[A].ChickenY:=random(SqY+1);

if Chickens[A].ChickenX=0 then Chickens[A].ChickenX:=1;
if Chickens[A].ChickenY=0 then Chickens[A].ChickenY:=1;

Chickens[A].PrevC:=GetColor(Chickens[A].ChickenX, Chickens[A].ChickenY);
Chickens[A].Dir:=1;

B:=Random(2+1);
If B=0 Then Chickens[A].ChickenType:=1;
If B=1 Then Chickens[A].ChickenType:=1;
If B=2 Then Chickens[A].ChickenType:=2;
end;
end;
end;
End;

Procedure TImage1.TickerSet(Value : Integer);
Begin
if Value <> AInterval then
begin
ATimer.Free;
ATimer := Nil;

ATimer:= TTimer.Create(Self);
ATimer.Interval := Value;
ATimer.OnTimer := MoveChickens;
AInterval := Value;
end;

End;

destructor TImage1.Destroy;
begin
Buffer.Free;
inherited Destroy;
end;


Procedure TImage1.SetX(Value : Integer);
Begin
SqX:=Value;
MakeGrid;
ColorGrid;

End;

Procedure TImage1.Paint;
var
A : Integer;
Begin

{Draw The Grid}
Canvas.Draw(0,0, Buffer);

End;

Procedure TImage1.SetY(Value : Integer);
Begin
SqY:=Value;
MakeGrid;
ColorGrid;
End;

Procedure TImage1.MakeGrid;
Var
A : Integer;
B : Integer;
X , Y : Integer;
TempRect : TRECT;
CL : Integer;
colorch : TColor;
begin
X:=0;
Y:=0;

Buffer.Width:=Width;
Buffer.Height:=Height;

SizeX:=Width div SqX;
SizeY:=Height div SqY;

temprect:=rect(0,0,Width,Height);
Canvas.StretchDraw(temprect, Buffer);
{Draw X Grid}

SizeX := SizeX + 1;
Sizey := Sizey + 1;

end;

Procedure TImage1.ColorGrid;
Var
X , Y : Integer;
X1, Y1: Integer;
A , B : Integer;
C , V : Integer;
temprect, temprec : TRECT;
CL, colorch : TColor;
Begin
X:=0;
Y:=0;
randomize;
For B:=1 to SqY do
Begin
For A:=1 to SqX do
begin
temprect:=rect(X,Y,X+SizeX,Y+SizeY);
Buffer.Canvas.Brush.Color:=clGray;
Buffer.Canvas.FillRect(temprect);
X:=X+SizeX;
End;
Y:=Y+SizeY;
X:=0;

End;

End;

Procedure TImage1.FillGrid(StartX : Integer ; StartY : Integer ; Color : TColor);
Var
SX, SY, EX, EY : Integer;
temprect : TRECT;
Begin
SX:=StartX * SizeX;
SY:=StartY * SizeY;
EX:=SX+SizeX;
EY:=SY+SizeY;

Buffer.Canvas.Brush.Color:=Color;
temprect:=rect(SX, SY, EX, EY);
Buffer.Canvas.FillRect(temprect);

End;


procedure TImage1.MoveChickens(Sender : TObject);
Var
A : Integer;
SX , SY, EX, EY : Integer;
temprect: TRect;
Dir : Integer; {1 Left, 2 Right, 3 Down, 4 Up, 5 = Current}
color : TColor;
tempcol : TColor;
TempInt : Integer;

{ This routine is where the Chickens are moved and the square is coloured in
{ The Chickens are not draw on the screen, only the behaviour i.e. the colouring of
the squares is shown}
Begin
For A:=1 to NoChicks do
Begin
{Grey Out the Current Square that the vehicle is on}
FillGrid(Chickens[a].ChickenX, Chickens[a].Chickeny,ClGray);

If Chickens[a].Dir=1 Then {Chickens Moving Left}
Begin
If Chickens[a].ChickenX-1<0 Then
Chickens[a].Dir:=4
Else
Begin
If CheckCollision(Chickens[a].ChickenX-1, Chickens[a].ChickenY)=False Then
Chickens[a].ChickenX:=Chickens[a].ChickenX-1
Else
Chickens[a].ChickenX:=Chickens[a].ChickenX
End;
End;

If Chickens[a].Dir=2 Then
Begin
If Chickens[a].ChickenX+1>Sqx-1 Then
Begin
Chickens[a].ChickenX:=SqX-1;
Chickens[a].Dir:=3
end
else
begin
if CheckCollision(Chickens[a].ChickenX+1, Chickens[a].Chickeny)=False Then
Chickens[a].ChickenX:=Chickens[a].ChickenX-1
Else
Chickens[a].ChickenX:=Chickens[a].ChickenX+1;

End;
End;
If Chickens[a].Dir=3 Then
Begin
If Chickens[a].Chickeny+1>SqY-1
Then
Chickens[a].Dir:=1
Else
Chickens[a].Chickeny:=Chickens[a].Chickeny+1;
End;

If Chickens[a].Dir=4 Then
Begin
If Chickens[a].Chickeny-1<0
Then
Chickens[a].Dir:=2
Else
Chickens[a].Chickeny:=Chickens[a].Chickeny-1;
End;

If Chickens[a].ChickenType=1 then
Begin
FillGrid(Chickens[a].ChickenX,Chickens[a].Chickeny, ClBlue);
End; {End Chick 1}

If Chickens[a].ChickenType=2 then
Begin
FillGrid(Chickens[a].ChickenX,Chickens[a].Chickeny, ClRed);
End;

End; {End For}

Paint;

End;

end.
======================================================================

AND the MainUnit

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unit MainUnit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ProcessUnit, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Image2: TImage;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Image1 : TImage1;
begin
//image2 := image1;

Image1:=TImage1.Create; //(Self);
Image1.NoOfChicks:=100;
Image1.MoveInterval:=200;
Image1.ChickensEnabled:=True;
//Image2 := Image1;

end;

end.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I would be very grateful if anyone could chat me abt the code on MSN messenger on basmah3@hotmai.com.

Kind Regards,
Basmah
Sign In or Register to comment.