fixedWidth = padding.left + padding.right;
int displayableComponentCount = 0;
for (int i = 0; i < n; i++) {
Component component = flowPane.get(i);
if (component.isDisplayable()) {
displayableComponentCount++;
}
}
if (displayableComponentCount > 1) {
fixedWidth += spacing * (displayableComponentCount - 1);
}
}
// Determine the starting x-coordinate
int componentX = 0;
switch (horizontalAlignment) {
case CENTER: {
componentX = (int)Math.round((double)(width - preferredWidth) / 2);
break;
}
case RIGHT: {
componentX = width - preferredWidth;
break;
}
}
componentX += padding.left;
// Lay out the components
for (int i = 0; i < n; i++) {
Component component = flowPane.get(i);
if (component.isDisplayable()) {
int componentWidth = 0;
int componentHeight = 0;
int componentY = 0;
// If the contents are horizontally justified, scale the
// component's width to match the available space
if (horizontalAlignment == HorizontalAlignment.JUSTIFY) {
if (width > fixedWidth
&& preferredWidth > fixedWidth) {
double widthScale = ((double)(width - fixedWidth)
/ (double)(preferredWidth - fixedWidth));
componentWidth = (int)Math.max(Math.round((double)component.getPreferredWidth(-1)
* widthScale), 0);
if (verticalAlignment == VerticalAlignment.JUSTIFY) {
componentY = padding.top;
componentHeight = Math.max(height - (padding.top
+ padding.bottom), 0);
} else {
componentHeight = component.getPreferredHeight(componentWidth);
}
}
} else {
if (verticalAlignment == VerticalAlignment.JUSTIFY) {
componentY = padding.top;
componentHeight = Math.max(height - (padding.top
+ padding.bottom), 0);
componentWidth = component.getPreferredWidth(componentHeight);
} else {
Dimensions preferredComponentSize = component.getPreferredSize();
componentWidth = preferredComponentSize.width;
componentHeight = preferredComponentSize.height;
}
}
switch (verticalAlignment) {
case TOP: {
componentY = padding.top;
break;
}
case CENTER: {
componentY = (int)Math.round((double)(height - componentHeight) / 2);
break;
}
case BOTTOM: {
componentY = height - padding.bottom
- componentHeight;
break;
}
}
// Set the component's size and position
component.setSize(componentWidth, componentHeight);
component.setLocation(componentX, componentY);
// Ensure that the component is visible
component.setVisible(true);
// Increment the x-coordinate
componentX += componentWidth + spacing;
} else {
// Hide the component
component.setVisible(false);
}
}
} else {
int preferredHeight = getPreferredHeight(width);
// Determine the fixed height (used in scaling components
// when justified vertically)
int fixedHeight = 0;
if (verticalAlignment == VerticalAlignment.JUSTIFY) {
fixedHeight = padding.top + padding.bottom;
int displayableComponentCount = 0;
for (int i = 0; i < n; i++) {
Component component = flowPane.get(i);
if (component.isDisplayable()) {
displayableComponentCount++;
}
}
if (displayableComponentCount > 1) {
fixedHeight += spacing * (displayableComponentCount - 1);
}
}
// Determine the starting y-coordinate
int componentY = 0;
switch (verticalAlignment) {
case CENTER: {
componentY = (int)Math.round((double)(height - preferredHeight) / 2);
break;
}
case BOTTOM: {
componentY = height - preferredHeight;
break;
}
}
componentY += padding.top;
// Lay out the components
for (int i = 0; i < n; i++) {
Component component = flowPane.get(i);
if (component.isDisplayable()) {
int componentWidth = 0;
int componentHeight = 0;
int componentX = 0;
if (horizontalAlignment == HorizontalAlignment.JUSTIFY) {
componentX = padding.left;
componentWidth = Math.max(width - (padding.left
+ padding.right), 0);
componentHeight = component.getPreferredHeight(componentWidth);
} else {
}
// If the contents are vertically justified, scale the
// component's height to match the available space
if (verticalAlignment == VerticalAlignment.JUSTIFY) {
if (height > fixedHeight
&& preferredHeight > fixedHeight) {
double heightScale = (double)(height - fixedHeight)
/ (double)(preferredHeight - fixedHeight);
componentHeight = (int)Math.max(Math.round((double)component.getPreferredHeight(-1)
* heightScale), 0);
if (horizontalAlignment == HorizontalAlignment.JUSTIFY) {
componentX = padding.left;
componentWidth = Math.max(width - (padding.left
+ padding.right), 0);
} else {
componentWidth = component.getPreferredWidth(componentHeight);
}
}
} else {
if (horizontalAlignment == HorizontalAlignment.JUSTIFY) {
componentX = padding.left;
componentWidth = Math.max(width - (padding.left
+ padding.right), 0);
componentHeight = component.getPreferredHeight(componentWidth);
} else {
Dimensions preferredComponentSize = component.getPreferredSize();
componentWidth = preferredComponentSize.width;
componentHeight = preferredComponentSize.height;
}
}
switch (horizontalAlignment) {
case LEFT: {
componentX = padding.left;
break;
}
case CENTER: {
componentX = (int)Math.round((double)(width - componentWidth) / 2);
break;
}
case RIGHT: {
componentX = width - padding.right
- componentWidth;
break;
}
}
// Set the component's size and position
component.setSize(componentWidth, componentHeight);
component.setLocation(componentX, componentY);
// Ensure that the component is visible
component.setVisible(true);
// Increment the y-coordinate
componentY += componentHeight + spacing;
} else {
// Hide the component
component.setVisible(false);
}
}
}
}