Question: Improve the picture gallery program in Section 8.5 to fill the space more efficiently. Instead of lining up all pictures along the top edge, find
Improve the picture gallery program in Section 8.5 to fill the space more efficiently. Instead of lining up all pictures along the top edge, find the first available space where you can insert a picture (still respecting the gaps).

Solve a simpler problem first, lining up the pictures without gaps.

That is still not easy. You need to test whether a new picture fits. Put the bounding rectangles of all placed pictures in an ArrayList and implement a method
![]()
that checks whether r intersects any of the given rectangles. Use the intersects method in the Rectangle class.
Then you need to figure out where you can try putting the new picture. Try something simple first, and check the corner points of all existing rectangles. Try points with smaller y-coordinates first. For a better fit, check all points whose x- and y-coordinates are the x- and y-coordinates of corner points, but not necessarily the same point.
Once that works, add the gaps between images.
•••
Step by Step Solution
3.34 Rating (160 Votes )
There are 3 Steps involved in it
To address the task described well have to modify the picture gallery program such that it can efficiently place images in a gallery while respecting given gaps and finding the first available space f... View full answer
Get step-by-step solutions from verified subject matter experts
