Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Jpeg;
- type
- TForm1 = class(TForm)
- imgPos1: TImage;
- imgPos2: TImage;
- imgPos3: TImage;
- imgPos4: TImage;
- imgPos5: TImage;
- imgPos6: TImage;
- procedure FormShow(Sender: TObject);
- procedure imgPos1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- lastClickedMovableImage: TImage; {hold the component that was clicked}
- imgNameArray: Tarray<string>; {array to know which image is in which component}
- const aImageFileName = 'E:\Delphi Projects\MoveImage\A.jpg';
- const whiteImageFileName = 'E:\Delphi Projects\MoveImage\white.jpg';
- implementation
- {$R *.dfm}
- procedure TForm1.FormShow(Sender: TObject);
- var
- appPath: String;
- posImg: TImage;
- pos: Integer;
- imgPosName: String;
- begin
- {you'll need this to point to the EXE path after the project is compiled
- to point to the image files included with the EXE}
- appPath := ExtractFileDir(Application.ExeName);
- {set the initial position images}
- for pos := 1 to 6 do
- begin
- {find each TImage component on the form}
- imgPosName := Concat('imgPos', pos.ToString());
- posImg := TImage(FindComponent(imgPosName));
- if (pos = 1) then
- begin {default position 1 as teh starting position for the image that will move}
- posImg.Picture.LoadFromFile(aImageFileName);
- end
- else
- begin
- posImg.Picture.LoadFromFile(whiteImageFileName);
- posImg.OnClick := imgPos1.OnClick; {assign all other TImage components the same event handler}
- end;
- end;
- imgNameArray := TArray<string>.Create('A','white','white','white','white','white');
- end;
- procedure TForm1.imgPos1Click(Sender: TObject);
- var
- imgName: String;
- clickedImg: TImage;
- imgPos : Integer;
- begin
- imgName := TImage(Sender).Name; {component name which contians its position on the form}
- imgPos := StrToInt(imgName.Substring(6));
- clickedImg := TImage(Sender);
- {what image is in this clicked TImage component}
- if imgNameArray[imgPos - 1] = 'A' then
- begin
- {this image can be moved}
- lastClickedMovableImage := clickedImg {remember this image}
- end
- else
- begin
- if lastClickedMovableImage <> nil then
- {a white space was clicked and there was a value in lastClickedImage
- meaning it was the image that can be moved}
- begin
- {move the A image from it's current location to the last clicked
- white space}
- clickedImg.Picture.LoadFromFile(aImageFileName);
- imgNameArray[imgPos - 1] := 'A';
- {put a white image in the previous location of the moveable image}
- lastClickedMovableImage.Picture.LoadFromFile(whiteImageFileName);
- imgName := lastClickedMovableImage.Name;
- imgPos := StrToInt(imgName.Substring(6));
- imgNameArray[imgPos - 1] := 'white';
- lastClickedMovableImage := nil;
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement