Hi everyone, it's been awhile, but I'm glad to be back.
I'm making a program to layout photos in a collage, grid style. I've created a custom component that extends JPanel, called PhotoPanel. Each PhotoPanel paints it's photo on the JPanel with a transparent background. I layout the PhotoPanels in a JPanel (called the PagePanel) using GridBagLayout. Each PhotoPanel has an instance of GridBagConstraints. When properties are changed for either the page or the selected PhotoPanel, the selected PhotoPanel's GridBagConstraints is updated, and the page layout is redone: e.g. all components are removed from the PagePanel and then readded using their own, updated GridBagConstraints object. The idea is that each photo can be set up to occupy multiple cells in the grid, using the GridBagConstraints 'gridwidth' and 'gridheight' properties. My method to add all of the PhotoPanels to the PagePanel keeps track of 'row', 'column', 'maxRows', and 'maxColumns' variables to control where the PhotoPanels are added. They are simply added in sequential order like so:
set the PhotoPanel's constraints, including 'row' and 'column'
page.add(PhotoPanel, PhotoPanel.myConstraints);
column++
if(column >= maxColumns){
column = 0;
row++;
}
What I want is to check the GridBagLayout to see if there is already a component occupying the cell at position "row,column" so that I can skip that cell. My current approach is that I create a boolean array of size [maxRows][maxColumns] and then set the "cells" where I add components to true. For components that occupy multiple cells on the PagePanel, multiple "cells" in the array are set to true in the proper positions. It works the way I want it to right now, but I feel like it would be more reliable to be able to "ask" the GridBagLayout if a certain cell is already occupied. Is there a way to do this? or should I just stick with my boolean array?