PDA

View Full Version : Position Problems



Cer3brus
01-04-2009, 11:29 AM
Its been bugging me like crazy that there isn't a .right and .bottom attribute to a TImage component(and quite a few other components strangely enough). I assumed .Right would be something like: Right := (component.left + component.width) and that bottom would be something simialer, although this doesn't seem to accurate. What are your thoughts?

Wizard
01-04-2009, 11:39 AM
Use the Height, Top, Width & Left properties :)

To move:

Left: Image.left := image.left -10
Up: Image.Top := image.top -10
Right: image.left := image.left + 10
Down: image.Top := image.top + 10

Cer3brus
01-04-2009, 11:46 AM
To get right and bottom?

Wizard
01-04-2009, 12:11 PM
To get the right I would imagine: Right := image.left + image.width and to get bottom would be Bottom := Image.top + image.height. Hope this helps :)

Something like:



procedure TForm1.BitBtn1Click(Sender: TObject);
var Right, Bottom : integer;
ResultRight, ResultBottom : string;
begin
Right := image1.Left + image1.Width;
Bottom := image1.Top + image1.Height;
ResultRight := IntToStr(Right);
ResultBottom := IntToStr(Bottom);
LabelRight.Caption := ResultRight;
LabelLeft.Caption := ResultBottom;
end;

Cer3brus
01-04-2009, 01:45 PM
It doesn't seem to work. :(
What I wanted to use the bottom and right for is to check whether an image component has gone off the screen.

In pseudo code:
Repeat
Image.Left/Top := Image.Left/Top + 1;// If this was -1 then 0 would suffice
Until Image has gone off the screen // when checking if the image had gone off screen

I hope it makes sense

efilnukefesin
01-04-2009, 02:04 PM
hi!

i would say you have to check the following four conditions:
1. the object can't leave the screen to the left
2. the object can't leave the screen to the top
3. the object can't leave the screen to the right
4. the object can't leave the screen to the bottom

i would suppose your 'object' is an image too, with a top/left and a height/width, and in my example 'landscape' is an image too:

checking routine (from head):


//move-code

//check positions
if object.left< landscape.left then begin
object.left:= landscape.left;
end;
if object.top< landscape.top then begin
object.top:= landscape.top;
end;
if object.left> landscape.left+ landscape.width- object.width then begin
object.left:= landscape.left+ landscape.width- object.width;
end;
if object.top> landscape.top+ landscape.height then begin
object.top:= landscape.top+ landscape.height- object.top;
end;

efilnukefesin
01-04-2009, 02:07 PM
and in the special case above, i would say it could be the following if i understood you right:



while (object.top<= landscape.top+ landscape.height) do begin
object.Top:= object.Top+ 1;
end;

Wizard
01-04-2009, 02:26 PM
Maybe something like this -- to check that the image doesn't move over the forms right or left border:



procedure TForm1.Timer1Timer(Sender: TObject);
begin
if (image1.Left >= Form1.Width - 200) then form1.x_Corner := true;
if (image1.Left <= 100) then form1.x_Corner := false;
if form1.x_Corner then
image1.Left := image1.Left - 100
else
image1.Left := image1.Left + 100;
end;


Where x_Corner : boolean; in your forms public declaration. The value 200 and 100 changes according to your image and form...

More or less the same for y (top & bottom)...

User137
01-04-2009, 02:36 PM
I will visualize...
Say we have TImage component with values:
.left:=2;
.width:=4;
So if i draw the coverage in pixels, it looks like:

01234567
..xxxx..

...which tells us that .right is at 5
So .right is 2+4-1 that is (.left + .width - 1)

And same logic in Y-direction .bottom = (.top + .height - 1)

Cer3brus
04-04-2009, 08:49 AM
Thanks!!
I'll try these out as soon as possible