?
Solved

Image click to return the name and position of that image ?

Posted on 2003-03-10
12
Medium Priority
?
261 Views
Last Modified: 2010-04-04
I have a panel with 16 small images 50 * 50.  They are 4 wide and 4 deep.
The last image has no picture (blank).  Images are named Image1 through to Image15 and the last one is named 'Imblank'.

I want to create a puzzel that when you click on an image next to the blanl one, the image and blank slide places.  Just like the number squares game (only I'm using a picture all diced up).

The GUI is all set up but the coding for this one I'm having difficulty with.

If you can't help with an idea, then can you tell me how to get the Image name returned to a variable once an image is clicked and to work out if the blank image is next to it and able to be moved there ?

Thanks

Mark
0
Comment
Question by:Mark Brady
[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
  • 6
  • 6
12 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 8101499
Hi Elvin,

I know the game you mean. Just an idea.
You have to create an array[0..3, 0..3] of integers and keep you data here.
Also you have create some visualization for array. I recommend you to use TDrawGrid.

It would be easy to implement.

____
Igor
0
 
LVL 9

Expert Comment

by:ITugay
ID: 8101625
here is a whole code of puzzle.

DG - TDrawGrid 4 x 4
Image1 - source image for puzzle, it will be splitted by 16 parts


Do not forget to assign an events :)

-----
Igor


unit u78;

interface

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

type
  TData16 = array[0..3, 0.. 3] of Integer;
  TImg16 = array[0..15] of TBitmap;

  TForm1 = class(TForm)
    DG: TDrawGrid;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure DGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
      State: TGridDrawState);
    procedure DGClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    Data: TData16;
    Imgs: TImg16;
    procedure PrepareImgs;
    procedure DoneImgs;
    procedure SetNewData;
    procedure ShowData;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.SetNewData;
var
  I, J, N: Integer;
  L: TList;
begin
  Randomize;
  L := TList.Create;
  for I := 0 to 15 do
    L.Add(Pointer(I));

  for I := 0 to 3 do
    for J := 0 to 3 do
    begin
      N := Random(L.Count);
      Data[I, J] := Integer(L[N]);
      L.Delete(N);
    end;

  L.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrepareImgs;
  SetNewData;
  DG.Invalidate;
end;

procedure TForm1.ShowData;
begin
end;

procedure TForm1.DGDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Data[ACol, ARow] = 0 then
    DG.Canvas.FillRect(Rect)
  else
    DG.Canvas.StretchDraw(Rect, Imgs[Data[ACol, ARow]]);
end;

procedure TForm1.DGClick(Sender: TObject);
var
  X, Y: Integer;

  procedure Move(AX, AY: Integer);
  begin
    Data[AX, AY] := Data[X, Y];
    Data[X, Y] := 0;
  end;

begin
  X := DG.Col;
  Y := DG.Row;

  if (X > 0) and (Data[X - 1, Y] = 0) then Move(X - 1, Y) else
  if (X < 3) and (Data[X + 1, Y] = 0) then Move(X + 1, Y) else
  if (Y > 0) and (Data[X, Y - 1] = 0) then Move(X, Y - 1) else
  if (Y < 3) and (Data[X, Y + 1] = 0) then Move(X, Y + 1) else
    Beep;
  DG.Invalidate;
end;

procedure TForm1.PrepareImgs;
var
  X, Y: Integer;
  W, H: Integer;
  I: Integer;
  B: TBitmap;
  RS, RD: TRect;
begin
  B := Image1.Picture.Bitmap;
  W := B.Width div 4;
  H := B.Height div 4;
  SetRect(RD, 0, 0, W, H);
  for X := 0 to 3 do
    for Y := 0 to 3 do
    begin
      SetRect(RS, W * X, H * Y, W * X + W, H * Y + H);
      I := 15 - (X + Y * 4);
      Imgs[I] := TBitmap.Create;
      Imgs[I].Width := W;
      Imgs[I].Height := H;
      Imgs[I].Canvas.CopyRect(RD, B.Canvas, RS);
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DoneImgs;
end;

procedure TForm1.DoneImgs;
var
  I: Integer;
begin
  for I := 0 to 15 do
    Imgs[I].Free;
end;

end.
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8109317
Hi ugay

Hey that looks real interesting.  It's a little over my head but I'd love to give it a try.  Can you tell me what I'd need on my form to make this work ?  Do I start with a blank project and paste this code into it ??  Buttons ?
Sorry, but I just don't know where to start.  Any more help would be very good.

thanks
Mark
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:ITugay
ID: 8109366
Yes, you can start from empty form

1. drop TDrawGrid and set it's property:
  Name = DG
  ColCount = 4
  RowCount = 4
  FixedRows = 0
  FixedCols = 0
  DefaultColWidth = your choice
  DefaultRowHeight = your choice

2.  Drop TImage and load any bitmap you want
 

3. Insert type definition

 TData16 = array[0..3, 0.. 3] of Integer;
 TImg16 = array[0..15] of TBitmap;

4. write procedures and implementations for TForm (you can copy and paste my code)

  public
    Data: TData16;
    Imgs: TImg16;
    procedure PrepareImgs;
    procedure DoneImgs;
    procedure SetNewData;
    procedure ShowData;
  end;


5. Create an events and place the same code as in sample here

    procedure FormCreate(Sender: TObject);
    procedure DGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
      State: TGridDrawState);
    procedure DGClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
 

Thats all.
Let me know if you still have a troubles.

____
Igor

0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8134365
I tried all that but I have a few problems.  It can't find
'Variants.dcu'.  Then I get a bunch of 'undeclared identifier' errors even though those things are on my form.

Any chance you could compile a quick example and send me the exe and the project files for me to study ?

If so, please send to elvin66@xtra.co.nz

Many thanks for your time.

Mark
0
 
LVL 9

Expert Comment

by:ITugay
ID: 8134743
hi Mark,

did you try to remove Variants from uses clause?

-----
Igor
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8140486
yes I did.  That solved that problem but still I get a lot of other errors.  It might be easier for me to study and customise If I could see a complete project file(s) and see how it runs.  If you don't want to send it, then can you be more clear on how to set it up ?  Like, with image1, do I place that next to the drawgrid or over top of it or what ?  
Mark
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8140493
Also, thius creates an error.

type
 TData16 = array[0..3, 0.. 3] of Integer;
error = ': expected but = found'  however I modify that line it doesn't work. Hmmm

Mark
0
 
LVL 9

Expert Comment

by:ITugay
ID: 8141716
Hi elvin66
I can post whole project to you,
but seems we are using different Delphi versions,
I have D6 source only. Not sure in backward compatibility.
Ok, I will try to send you only .DFM and .PAS files.
------
Igor
0
 
LVL 9

Accepted Solution

by:
ITugay earned 500 total points
ID: 8141721
u78.pas
---------------------------------------------------------------------------
unit u78;

interface

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

type
  TData16 = array[0..3, 0.. 3] of Integer;
  TImg16 = array[0..15] of TBitmap;

  TForm1 = class(TForm)
    DG: TDrawGrid;
    Image1: TImage;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure DGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
      State: TGridDrawState);
    procedure DGClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    Data: TData16;
    Imgs: TImg16;
    procedure PrepareImgs;
    procedure DoneImgs;
    procedure SetNewData;
    procedure ShowData;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.SetNewData;
var
  I, J, N: Integer;
  L: TList;
begin
  Randomize;
  L := TList.Create;
  for I := 0 to 15 do
    L.Add(Pointer(I));

  for I := 0 to 3 do
    for J := 0 to 3 do
    begin
      N := Random(L.Count);
      Data[I, J] := Integer(L[N]);
      L.Delete(N);
    end;

  L.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  PrepareImgs;
  SetNewData;
  DG.Invalidate;
end;

procedure TForm1.ShowData;
begin
end;

procedure TForm1.DGDrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Data[ACol, ARow] = 0 then
    DG.Canvas.FillRect(Rect)
  else
    DG.Canvas.StretchDraw(Rect, Imgs[Data[ACol, ARow]]);
end;

procedure TForm1.DGClick(Sender: TObject);
var
  X, Y: Integer;

  procedure Move(AX, AY: Integer);
  begin
    Data[AX, AY] := Data[X, Y];
    Data[X, Y] := 0;
  end;

begin
  X := DG.Col;
  Y := DG.Row;

  if (X > 0) and (Data[X - 1, Y] = 0) then Move(X - 1, Y) else
  if (X < 3) and (Data[X + 1, Y] = 0) then Move(X + 1, Y) else
  if (Y > 0) and (Data[X, Y - 1] = 0) then Move(X, Y - 1) else
  if (Y < 3) and (Data[X, Y + 1] = 0) then Move(X, Y + 1) else
    Beep;
  DG.Invalidate;
end;

procedure TForm1.PrepareImgs;
var
  X, Y: Integer;
  W, H: Integer;
  I: Integer;
  B: TBitmap;
  RS, RD: TRect;
begin
  B := Image1.Picture.Bitmap;
  W := B.Width div 4;
  H := B.Height div 4;
  SetRect(RD, 0, 0, W, H);
  for X := 0 to 3 do
    for Y := 0 to 3 do
    begin
      SetRect(RS, W * X, H * Y, W * X + W, H * Y + H);
      I := 15 - (X + Y * 4);
      Imgs[I] := TBitmap.Create;
      Imgs[I].Width := W;
      Imgs[I].Height := H;
      Imgs[I].Canvas.CopyRect(RD, B.Canvas, RS);
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DoneImgs;
end;

procedure TForm1.DoneImgs;
var
  I: Integer;
begin
  for I := 0 to 15 do
    Imgs[I].Free;
end;

procedure Proc1(var M: Integer);
begin
  M := 1000;
end;

procedure DummyProc;
begin

end;



procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  A: Pointer;
  L: Integer;
  CS: Integer;
begin
  A := @Proc1;
  L := Integer(@DummyProc) - Integer(@Proc1);
  CS := 0;
  while L > 0 do
  begin
    CS := CS + Byte(A^);
    dec(L);
    A := Pointer(Integer(A) + 1);
  end;
  ShowMessage(IntToStr(CS));
end;

end.

u78.DFM
----------------------------------------------------------------------------

object Form1: TForm1
  Left = 190
  Top = 136
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 266
    Top = 38
    Width = 129
    Height = 129
    Picture.Data = {
      07544269746D6170F6040000424DF60400000000000076000000280000003000
      000030000000010004000000000080040000C30E0000C30E0000100000000000
      0000000000000000800000800000008080008000000080008000808000008080
      8000C0C0C0000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFF
      FF004444444C44444444C4C444444444C4C44444444C444444444444CCC44444
      444C44C444444444C44C44444444CCC4444444CC44444444444C4C4444444444
      4C4C44444444444CC4444C444444444444C44C44444444444C44C44444444444
      4C444C444CCCC444CC44C4444444444444C44CC444CCCC444C444C44C4444CCC
      444C444444444444444C444CCC4444C44C44C44C4444444444C44444444C4444
      4444C4444444444C44C4C4C4444CC444CC44444444CCC44444444CC444CC4444
      C4C4CC4C44C44CCC44444444CCC4CCC44444444CCC44C44C4CC44444C44C4444
      4444444CC44C44CC44444444444C44C4444C4444C44C4444444444CC4CCCCC4C
      C4444444444C44C444444444C44C444444444CC4CCCCCCC4CC444444444C44C4
      4444444C44C4444444444CC4CCCCCCC4CC4444444444C44C4444444C44C44444
      4444CC4CCCCCCCCC4CC444444444C44C444444C44C4444444444CC4CCCCCCCCC
      4CC4444444444C44C444CC44C44444444444CC4CCCC4CCCC4CC44444444444C4
      4CC4444C4444444444444CC4CC4C4CC4CC4444444444444C444C4CC44444444C
      CC44CCCC4C4C4C4CCCC44CCC44444444CC44C44444444CCCCCCCCCC4CC4C4CC4
      CCCCCCCCCC44444444C444444444CCC444CCCC4CC4CCC4CC4CCCC444CCC44444
      444C4444444CC44CCC4444CCC4CCC4CCC4444CCC44CC44444444444444CC4CCC
      CCCCCCCCC4CCC4CCCCCCCCCCCC4CC4444444444444C4CCCCCCCCCCC44CCCCC44
      CCCCCCCCCCC4C444444444444CC4CCCCCCCC444CCCCCCCCC444CCCCCCCC4CC44
      444444444C4CCCCCCCC4CCCCCCCCCCCCCCC4CCCCCCCC4C44444444444CC4CCCC
      CCCC444CCCCCCCCC444CCCCCCCC4CC444444444444C4CCCCCCCCCCC44CCCCC44
      CCCCCCCCCCC4C4444444444444CC4CCCCCCCCCCCC4CCC4CCCCCCCCCCCC4CC444
      44444444444CC44CCC4444CCC4CCC4CCC4444CCC44CC4444444444444444CCC4
      44CCCC4CC4CCC4CC4CCCC444CCC44444444CC44444444CCCCCCCCCC4CC4C4CC4
      CCCCCCCCCC44444444C44CC44444444CCC44CCCC4C4C4C4CCCC44CCC44444444
      CC44444C4444444444444CC4C44C44C4CC4444444444444C444CCC44C4444444
      4444CC4CCCC4CCCC4CC44444444444C44CC444C44C4444444444CC4CCCCCCCCC
      4CC4444444444C44C444444C44C444444444CC4CCCCCCCCC4CC444444444C44C
      4444444C44C4444444444CC4CCCCCCC4CC4444444444C44C44444444C44C4444
      44444CC4CCCCCCC4CC444444444C44C444444444C44C4444444444CC4CCCCC4C
      C4444444444C44C444444444C44C44444444444CC44C44CC44444444444C44C4
      444CC44C44C44CCC44444444CCC4CCC44444444CCC44C44C44C4C4C4444CC444
      CC44444444CCC44444444CC444CC4444C4C4C44C4444444444C44444444C4444
      4444C4444444444C44C44C44C4444CCC444C444444444444444C444CCC4444C4
      4C444C444CCCC444CC44C4444444444444C44CC444CCCC444C444C4444444444
      44C44C44444444444C44C444444444444C4444CC44444444444C4C4444444444
      4C4C44444444444CC4444444CCC44444444C44C444444444C44C44444444CCC4
      4444}
  end
  object SpeedButton1: TSpeedButton
    Left = 50
    Top = 234
    Width = 23
    Height = 22
    OnClick = SpeedButton1Click
  end
  object DG: TDrawGrid
    Left = 40
    Top = 34
    Width = 135
    Height = 135
    ColCount = 4
    DefaultColWidth = 32
    DefaultRowHeight = 32
    DefaultDrawing = False
    FixedCols = 0
    RowCount = 4
    FixedRows = 0
    TabOrder = 0
    OnClick = DGClick
    OnDrawCell = DGDrawCell
  end
end
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8145090
that would be great.  I'll check my email.  I'm using delphi 5
0
 
LVL 20

Author Comment

by:Mark Brady
ID: 8145274
That works perfectly !  Never mind the email, I got it working and changed the bitmap to the picture I want to use.  Now I will go and put a button on the form that lets user load different images.  Thank you so much !

Mark
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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