?
Solved

Drawing a grid of images

Posted on 2003-02-28
3
Medium Priority
?
256 Views
Last Modified: 2010-04-04
I have a grid of 7 x 7 TImages, and I want to randomly assign different images to each on on formcreate. At the moment I have the following code:
function LoadRandTile: TBitmap;
var X : Integer;
begin
  Randomize;
  LoadRandTile := TBitmap.Create;
  try
  X := Random(15);
  If X = 0 then
    LoadRandTile.LoadFromFile('gfx\Tile_S1.bmp')
  else if X = 1 then
    LoadRandTile.LoadFromFile('gfx\Tile_S2.bmp')
  else if X = 2 then
    LoadRandTile.LoadFromFile('gfx\Tile_S3.bmp')
  else if X = 3 then
    LoadRandTile.LoadFromFile('gfx\Tile_S4.bmp')
  else if X = 4 then
    LoadRandTile.LoadFromFile('gfx\Tile_C1.bmp')
  else if X = 5 then
    LoadRandTile.LoadFromFile('gfx\Tile_C2.bmp')
  else if X = 6 then
    LoadRandTile.LoadFromFile('gfx\Tile_C3.bmp')
  else if X = 7 then
    LoadRandTile.LoadFromFile('gfx\Tile_C4.bmp')
  else if X = 8 then
    LoadRandTile.LoadFromFile('gfx\Tile_T1.bmp')
  else if X = 9 then
    LoadRandTile.LoadFromFile('gfx\Tile_T2.bmp')
  else if X = 10 then
    LoadRandTile.LoadFromFile('gfx\Tile_T3.bmp')
  else if X = 11 then
    LoadRandTile.LoadFromFile('gfx\Tile_T4.bmp')
  else if X = 12 then
    LoadRandTile.LoadFromFile('gfx\Tile_X1.bmp')
  else if X = 13 then
    LoadRandTile.LoadFromFile('gfx\Tile_X2.bmp')
  else if X = 14 then
    LoadRandTile.LoadFromFile('gfx\Tile_X3.bmp')
  else if X = 15 then
    LoadRandTile.LoadFromFile('gfx\Tile_X4.bmp')
  finally
  //LoadRandTile.Free
  end;  
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  LoadRandTile();
  B2x1.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B3x1.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B4x1.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B5x1.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B6x1.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B1x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B2x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B3x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B4x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B5x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B6x2.Picture.Assign(LoadRandTile);
  LoadRandTile();
  B7x2.Picture.Assign(LoadRandTile);
  etc...
end;

Now there are 2 problems with that.
1) It takes ages to type out the asigning of each grid space.
2) The images dont appear to be random. I tried using a ShowMessage command to show the random number, and they appear to be random, but it is not freeing the image handle. However, whenever I try to insert the line:
LoadRandTile.Free;
It comes up with an error during run time of:
Project ProjectName.exe raised execption class EAccessViolation with message 'Access violation at address 00000007. Write of address 0044E480'. Process stopped. Use Step or Run to contine.
And it has stopped at the point where I have assigned LoadRandTile to the TImage. If I click Run again it crashed and returns to to the Delphi environment.
0
Comment
Question by:icr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
ILE earned 200 total points
ID: 8045529
complete working solution if u must stay with that names  :))

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
  r:real;
begin
i:=trunc(time*360000) mod 100;
for i:=0 to i do
r:=random(i)+r;              {this is random randomize :))}
end;

function LoadRandTile: TBitmap;
var X : Integer;
    s:string;
begin
  s:='SCTX';
 LoadRandTile := TBitmap.Create;
   LoadRandTile.LoadFromFile('gfx\Tile_'+S[random(4)+1]+inttostr(random(4)+1)+'.bmp')
end;

procedure TForm1.FormActivate(Sender: TObject);
var i:integer;
begin
 for i:=0 to form1.componentcount-1 do
   if (form1.Components[i] is timage) then
     begin
       (form1.Components[i] as timage).picture.Assign(loadrandtile());
     end;
end;


easy arent ?
0
 
LVL 7

Author Comment

by:icr
ID: 8047365
Thanks alot. It works perfectly. I wouldnt have thought of doing most of it that way, but reading through it it makes total sense. Thanks. I would perhaps give you more points, but I don't have them :(
0
 
LVL 3

Expert Comment

by:ILE
ID: 8048357
my pleasure
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question