StackedLayout extends AbstractOrientedLayout { public Dimension2D getPreferredSize(Container container) { Insets2D insets = container.getInsets(); double width = insets.getLeft(); double height = insets.getTop(); int count = 0; if (getOrientation() == Orientation.HORIZONTAL) { double h = 0.0; for (Drawable component : container) { if (count++ > 0) { width += getGapX(); } Dimension2D itemBounds = component.getPreferredSize(); width += itemBounds.getWidth(); h = Math.max(height, itemBounds.getHeight()); } height += h; } else if (getOrientation() == Orientation.VERTICAL) { double w = 0.0; for (Drawable component : container) { if (count++ > 0) { height += getGapY(); } Dimension2D itemBounds = component.getPreferredSize(); w = Math.max(w, itemBounds.getWidth()); height += itemBounds.getHeight(); } width += w; } width += insets.getRight(); height += insets.getBottom(); return new de.erichseifert.gral.graphics.Dimension2D.Double(width, height); } StackedLayout(Orientation orientation);  StackedLayout(Orientation orientation, double gapX, double gapY); void layout(Container container); Dimension2D getPreferredSize(Container container);  }
@Test public void testPreferredSizeVertical() { Layout layout = new StackedLayout(Orientation.VERTICAL, GAP_X, GAP_Y); Dimension2D size = layout.getPreferredSize(container); assertEquals(COMP_WIDTH, size.getWidth(), DELTA); assertEquals(3.0*COMP_HEIGHT + 2.0*GAP_Y, size.getHeight(), DELTA); }
@Test public void testPreferredSizeHorizontal() { Layout layout = new StackedLayout(Orientation.HORIZONTAL, GAP_X, GAP_Y); Dimension2D size = layout.getPreferredSize(container); assertEquals(3.0*COMP_WIDTH + 2.0*GAP_X, size.getWidth(), DELTA); assertEquals(COMP_HEIGHT, size.getHeight(), DELTA); }