How to convert panel coordinates to rectangle coordinates?

positrom2

Active Member
Licensed User
Longtime User
Using Designer I have a panel with coordinates defined by (left,top,width,height). I want to create a rectangle covering the panel. Unfortunately, the rectangle properties are defined differently as (left,top,right,bottom), and I am struggling to get the right values from the panel properties. How to get the rectangle properties from the panel properties?
Thanks, positrom2
 

Mahares

Expert
Licensed User
Longtime User
I think this is what you are looking for. If not, forgive me:
B4X:
Rectangle.left=Panel.Left
Rectangle.Top=Panel.Top
Rectangle.Right=Panel.Left + Panel.Width
Rectangle.Bottom=Panel.Top + Panel.Height
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Thanks, Mahares,
I had already done as you suggested, but it does not work (the panel color does not change to black).

The panel properties in designer are:
left=0, top=480,width=220,height=300.
The code looks like
B4X:
Dim pnlpeaks As Panel
Dim rect1 As Rect
cvspeaks.Initialize(pnlpeaks)
rect1.Initialize(0, 480, 220, 780)
cvspeaks.DrawRect(rect1, Colors.Black, True, 1)
Activity.invalidate

The color of the panel does not change.

With another rectangle, on a different canvas, painting the panel in black works. But there I had initialized the rectangle for a panel with coordinates
left=220,top=310,width=380,height=470 by
B4X:
rect2.Initialize(0, 0, 100%x, 100%y)
Is there a misunderstanding in the coordinate designations?

Thanks, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Erel, thanks, but:
You should always use dip units when specifying pixel measurements.
but: The beginner's guide tells that
The values for the Left, Top, Width and Height properties in the Designer are considered as
dip values.
I also tried
B4X:
rect1.Initialize(0, 480dip, 220dip, 780dip)
with the rect coordinates read out from designer, but it does not give the expected result, whereas (0,0,100%x,100%y) is allright!

So it is unclear to me how the coordinate system for rectangle definition is defined and what dip means in the rectangle definition.

Thanks, positrom2
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@positrom: I created on the designer a layout called Rectangle 480x800, with scale:1. I put a panel on it called: pnlPeaks. The panel has the same dimensions as yours: L=0, T=480, W=220 and H=300. See below code where I was able to overlap the rectangle to the panel background. I hope this is what you are looking to achieve.

B4X:
Dim Rect1 As Rect
Dim pnlPeaks As Panel
Dim cvspeaks As Canvas

Activity.LoadLayout("Rectangle")
cvspeaks.Initialize(pnlPeaks)      'Canvas is drawn on panel background
Rect1.Initialize(0,0,220dip,300dip)
cvspeaks.DrawRect(Rect1,Colors.Red,True,0dip)
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Mahares,
Yes, that works. So what I learn is that rectangles drawn on a panel will have their origin in the left, top corner of the panel. Then R=W and B=H which is easy. Maybe I missed that rule when I searched on this issue.
Thanks, positrom2
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…