List of usage examples for java.awt Container getSize
public Dimension getSize()
From source file:org.kineticsystem.commons.layout.TetrisLayout.java
/** * Lay out components in the specified container. * @param parent The container which needs to be laid out. *///from www . j a v a 2 s . c o m public void layoutContainer(Container parent) { /* * This method synchronizes on the tree lock of the component. This tree * lock is an object that can be used to provide thread-safe access to * the layout manager in case different threads are simultaneously * adding or removing components. The tree lock object is used as a * synchronization point for all of the methods associated with laying * out containers and invalidating components, and it is good * programming practice to use it to ensure a thread-safe * implementation. */ synchronized (parent.getTreeLock()) { // Layout components. if (containerSize == null) { setup(parent); } Insets insets = parent.getInsets(); int componentNumber = parent.getComponentCount(); if (componentNumber == 0) { return; } Dimension size = parent.getSize(); cGaps[0].setIncSize(cGaps[0].getSize()); for (int i = 1; i < cBars.length + 1; i++) { cGaps[i].setIncSize(cGaps[i].getSize() + cGaps[i - 1].getIncSize()); } rGaps[0].setIncSize(rGaps[0].getSize()); for (int i = 1; i < rBars.length + 1; i++) { rGaps[i].setIncSize(rGaps[i].getSize() + rGaps[i - 1].getIncSize()); } int actualGapFreeWidth = Math .max(size.width - insets.left - insets.right - cGaps[cBars.length].getIncSize(), 0); int actualGapFreeHeight = Math .max(size.height - insets.top - insets.bottom - rGaps[rBars.length].getIncSize(), 0); for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) { Component comp = (Component) entry.getKey(); Cell cell = (Cell) entry.getValue(); if (cell != null) { // Actual cell position. int x0 = (int) Math.round(cSizes[cell.getCol()].evaluate(actualGapFreeWidth)); int y0 = (int) Math.round(rSizes[cell.getRow()].evaluate(actualGapFreeHeight)); int x1 = (int) Math.round(cSizes[cell.getCol() + cell.getCols()].evaluate(actualGapFreeWidth)); int y1 = (int) Math.round(rSizes[cell.getRow() + cell.getRows()].evaluate(actualGapFreeHeight)); // Actual cell dimension. int w = x1 - x0; int h = y1 - y0; // Component position correction. int xCorrection = insets.left + cGaps[cell.getCol()].getIncSize(); int yCorrection = insets.top + rGaps[cell.getRow()].getIncSize(); // Component dimension correction. int wCorrection = cGaps[cell.getCol() + cell.getCols() - 1].getIncSize() - cGaps[cell.getCol()].getIncSize(); int hCorrection = rGaps[cell.getRow() + cell.getRows() - 1].getIncSize() - rGaps[cell.getRow()].getIncSize(); // Preferred component dimension. int wComp = comp.getPreferredSize().width; int hComp = comp.getPreferredSize().height; int fill = cell.getFill(); int anchor = cell.getAnchor(); switch (fill) { case Cell.NONE: { if (wComp > w) { wComp = w; } if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + h - hCorrection - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.PROPORTIONAL: { double ratio = Math.min((double) ((double) w / (double) wComp), (double) ((double) h / (double) hComp)); wComp = (int) Math.round(wComp * ratio); hComp = (int) Math.round(hComp * ratio); switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + hCorrection + h - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.BOTH: { x0 += xCorrection; y0 += yCorrection; w += wCorrection; h += hCorrection; break; } case Cell.HORIZONTAL: { if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.PAGE_START: case Cell.FIRST_LINE_END: y0 += yCorrection; break; case Cell.LINE_START: case Cell.CENTER: case Cell.LINE_END: y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: case Cell.PAGE_END: case Cell.LAST_LINE_END: y0 += yCorrection + h + hCorrection - hComp; break; } x0 += xCorrection; w += wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.VERTICAL: { if (wComp > w) { wComp = w; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.LINE_START: case Cell.LAST_LINE_START: x0 += xCorrection; break; case Cell.PAGE_START: case Cell.CENTER: case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; break; case Cell.FIRST_LINE_END: case Cell.LINE_END: case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; break; } y0 += yCorrection; w = Math.min(w, wComp) + wCorrection; h += hCorrection; break; } } comp.setBounds(x0, y0, w, h); } } } }
From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.java
/** * Positions the specified dialog at a position relative to its parent. * * @param dialog the dialog to be positioned. * @param horizontalPercent the relative location. * @param verticalPercent the relative location. *//*from w w w. j a va 2s .c om*/ public static void positionDialogRelativeToParent(final Dialog dialog, final double horizontalPercent, final double verticalPercent) { final Container parent = dialog.getParent(); if (parent == null || (parent.isVisible() == false)) { positionFrameOnScreen(dialog, horizontalPercent, verticalPercent); return; } final Dimension d = dialog.getSize(); final Dimension p = parent.getSize(); final int baseX = parent.getX(); final int baseY = parent.getY(); final int parentPointX = baseX + (int) (horizontalPercent * p.width); final int parentPointY = baseY + (int) (verticalPercent * p.height); final int dialogPointX = parentPointX - (int) (horizontalPercent * d.width); final int dialogPointY = parentPointY - (int) (verticalPercent * d.height); // make sure the dialog fits completely on the screen... final Rectangle s = parent.getGraphicsConfiguration().getBounds(); final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height); final Rectangle intersectedDialogBounds = r.intersection(s); if (intersectedDialogBounds.width < d.width) { r.x = s.width - d.width; r.width = d.width; } if (intersectedDialogBounds.height < d.height) { r.y = s.height - d.height; r.height = d.height; } final Rectangle finalIntersection = r.intersection(s); dialog.setBounds(finalIntersection); }