?
Solved

Drawing a grid of images

Posted on 2003-02-28
3
Medium Priority
?
257 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
  • 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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

578 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