Improve the picture gallery program in Section 8.5 to fill the space more efficiently. Instead of lining
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 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 Answer: