Add support for customizable gap and border

Add support for customizable gap and border to PanelComponent.

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-05-07 14:40:03 +02:00
parent fa8667fa2c
commit 5bce0e43fc
2 changed files with 24 additions and 14 deletions

View File

@@ -28,7 +28,7 @@ import java.awt.Color;
public class ComponentConstants public class ComponentConstants
{ {
public static final int STANDARD_PADDING = 4; public static final int STANDARD_BORDER = 4;
public static final int STANDARD_WIDTH = 129; public static final int STANDARD_WIDTH = 129;
public static final Color STANDARD_BACKGROUND_COLOR = new Color(70, 61, 50, 156); public static final Color STANDARD_BACKGROUND_COLOR = new Color(70, 61, 50, 156);
} }

View File

@@ -28,6 +28,7 @@ import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -42,11 +43,6 @@ public class PanelComponent implements LayoutableRenderableEntity
VERTICAL; VERTICAL;
} }
private static final int TOP_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int LEFT_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int RIGHT_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int BOTTOM_BORDER = ComponentConstants.STANDARD_PADDING;
@Setter @Setter
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR; private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
@@ -59,6 +55,16 @@ public class PanelComponent implements LayoutableRenderableEntity
@Setter @Setter
private Orientation orientation = Orientation.VERTICAL; private Orientation orientation = Orientation.VERTICAL;
@Setter
private Rectangle border = new Rectangle(
ComponentConstants.STANDARD_BORDER,
ComponentConstants.STANDARD_BORDER,
ComponentConstants.STANDARD_BORDER,
ComponentConstants.STANDARD_BORDER);
@Setter
private Point gap = new Point(0, 0);
private final Dimension childDimensions = new Dimension(); private final Dimension childDimensions = new Dimension();
@Override @Override
@@ -73,8 +79,8 @@ public class PanelComponent implements LayoutableRenderableEntity
// Render background // Render background
final Dimension dimension = new Dimension( final Dimension dimension = new Dimension(
LEFT_BORDER + childDimensions.width + RIGHT_BORDER, border.x + childDimensions.width + border.width,
TOP_BORDER + childDimensions.height + BOTTOM_BORDER); border.y + childDimensions.height + border.height);
final BackgroundComponent backgroundComponent = new BackgroundComponent(); final BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setRectangle(new Rectangle(dimension)); backgroundComponent.setRectangle(new Rectangle(dimension));
@@ -82,8 +88,8 @@ public class PanelComponent implements LayoutableRenderableEntity
backgroundComponent.render(graphics); backgroundComponent.render(graphics);
// Offset children // Offset children
final int baseX = LEFT_BORDER; final int baseX = border.x;
final int baseY = TOP_BORDER + metrics.getHeight(); final int baseY = border.y + metrics.getHeight();
int width = 0; int width = 0;
int height = 0; int height = 0;
int x = baseX; int x = baseX;
@@ -91,8 +97,8 @@ public class PanelComponent implements LayoutableRenderableEntity
// Create child preferred size // Create child preferred size
final Dimension childPreferredSize = new Dimension( final Dimension childPreferredSize = new Dimension(
preferredSize.width - LEFT_BORDER - RIGHT_BORDER, preferredSize.width - border.x - border.width,
preferredSize.height - TOP_BORDER - BOTTOM_BORDER); preferredSize.height - border.y - border.height);
// Render all children // Render all children
for (final LayoutableRenderableEntity child : children) for (final LayoutableRenderableEntity child : children)
@@ -105,18 +111,22 @@ public class PanelComponent implements LayoutableRenderableEntity
switch (orientation) switch (orientation)
{ {
case VERTICAL: case VERTICAL:
height += childDimension.height; height += childDimension.height + gap.y;
y = baseY + height; y = baseY + height;
width = Math.max(width, childDimension.width); width = Math.max(width, childDimension.width);
break; break;
case HORIZONTAL: case HORIZONTAL:
width += childDimension.width; width += childDimension.width + gap.x;
x = baseX + width; x = baseX + width;
height = Math.max(height, childDimension.height); height = Math.max(height, childDimension.height);
break; break;
} }
} }
// Remove last child gap
width -= gap.x;
height -= gap.y;
// Cache children bounds // Cache children bounds
childDimensions.setSize(width, height); childDimensions.setSize(width, height);