I guess greybeard knows better about computational geometry, yet, as a novice, I would do the next: sort all the rectangles by area. Then start layoutlaying them out from left to right to the top of the panel. When the first "row" is occupied, lay the next "row" from right to left, and so on.