Java Code Examples for java.awt.Container#getX()

The following examples show how to use java.awt.Container#getX() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefineryUtilities.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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.
 */
public static void positionDialogRelativeToParent(final Dialog dialog,
                                                  final double horizontalPercent,
                                                  final double verticalPercent) {
  final Container parent = dialog.getParent();
  if (parent == null)
  {
    centerFrameOnScreen(dialog);
    return;
  }

  final Dimension d = dialog.getSize();
  final Dimension p = parent.getSize();

  final int baseX = parent.getX();
  final int baseY = parent.getY();

  final int x = baseX + (int) (horizontalPercent * p.width);
  final int y = baseY + (int) (verticalPercent * p.height);

  // make sure the dialog fits completely on the screen...
  final Rectangle s = parent.getGraphicsConfiguration().getBounds();
  final Rectangle r = new Rectangle(x, y, d.width, d.height);
  dialog.setBounds(r.intersection(s));
}
 
Example 2
Source File: RefineryUtilities.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public static void positionDialogRelativeToParent(Dialog dialog,
                                                  double horizontalPercent,
                                                  double verticalPercent) {
    Dimension d = dialog.getSize();
    Container parent = dialog.getParent();
    Dimension p = parent.getSize();

    int baseX = parent.getX() - d.width;
    int baseY = parent.getY() - d.height;
    int w = d.width + p.width;
    int h = d.height + p.height;
    int x = baseX + (int) (horizontalPercent * w);
    int y = baseY + (int) (verticalPercent * h);

    // make sure the dialog fits completely on the screen...
    Rectangle s = getMaximumWindowBounds();
    x = Math.min(x, (s.width - d.width));
    x = Math.max(x, 0);
    y = Math.min(y, (s.height - d.height));
    y = Math.max(y, 0);

    dialog.setBounds(x + s.x, y + s.y, d.width, d.height);

}
 
Example 3
Source File: RefineryUtilities.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public static void positionDialogRelativeToParent(Dialog dialog,
                                                  double horizontalPercent,
                                                  double verticalPercent) {
    Dimension d = dialog.getSize();
    Container parent = dialog.getParent();
    Dimension p = parent.getSize();

    int baseX = parent.getX() - d.width;
    int baseY = parent.getY() - d.height;
    int w = d.width + p.width;
    int h = d.height + p.height;
    int x = baseX + (int) (horizontalPercent * w);
    int y = baseY + (int) (verticalPercent * h);

    // make sure the dialog fits completely on the screen...
    Rectangle s = getMaximumWindowBounds();
    x = Math.min(x, (s.width - d.width));
    x = Math.max(x, 0);
    y = Math.min(y, (s.height - d.height));
    y = Math.max(y, 0);

    dialog.setBounds(x + s.x, y + s.y, d.width, d.height);

}
 
Example 4
Source File: SwingUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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.
 */
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 = Math.max( 0, parentPointX - (int) ( horizontalPercent * d.width ) );
  final int dialogPointY = Math.max( 0, 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 );
}
 
Example 5
Source File: BufferedCanvasComponent.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected boolean canDirectlyAccessGraphics() {
        // TODO: what about popup windows / tooltips???

        // TODO: some of the queries could be cached instead of polling,
        // for example isShowing(), isOpaque(), getParent() etc.

//////        // Shouldn't access graphics - no buffering would cause flickering
//////        if (bufferType == BUFFER_NONE) return false;

        // Cannot access graphics - there are some child components
        if (getComponentCount() != 0) return false;

        // Cannot access graphics - component doesn't fully control its area
        if (!isOpaque()) return false;

        // Cannot access graphics - not in Swing tree
        if (!(getParent() instanceof JComponent)) return false;

        // Cannot access graphics - component not showing, doesn't make sense
        if (!isShowing()) return false;

        // Cannot access graphics - component area is not up-to-date
        Rectangle dirtyRegion = RepaintManager.currentManager(this).
                                getDirtyRegion((JComponent)getParent());
        if (dirtyRegion != null && dirtyRegion.width > 0 &&
            dirtyRegion.height > 0) return false;

        // --- Reused from JViewport -------------------------------------------

        Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
        Rectangle oldClip = new Rectangle();
        Rectangle tmp2 = null;
        Container parent;
        Component lastParent = null;
        int x, y, w, h;

        for (parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
            x = parent.getX();
            y = parent.getY();
            w = parent.getWidth();
            h = parent.getHeight();

            oldClip.setBounds(clip);
            SwingUtilities.computeIntersection(0, 0, w, h, clip);
            if (!clip.equals(oldClip)) return false;

            if (lastParent != null && parent instanceof JComponent &&
               !((JComponent)parent).isOptimizedDrawingEnabled()) {
                Component comps[] = parent.getComponents();
                int index = 0;

                for (int i = comps.length - 1 ;i >= 0; i--) {
                    if (comps[i] == lastParent) {
                    index = i - 1;
                    break;
                    }
                }

                while (index >= 0) {
                    tmp2 = comps[index].getBounds(tmp2);
                    if (tmp2.intersects(clip)) return false;
                    index--;
                }
            }
            clip.x += x;
            clip.y += y;
            lastParent = parent;
        }

        // No Window parent.
        if (parent == null) return false;

        return true;
    }
 
Example 6
Source File: BufferedCanvasComponent.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
protected boolean canDirectlyAccessGraphics() {
        // TODO: what about popup windows / tooltips???

        // TODO: some of the queries could be cached instead of polling,
        // for example isShowing(), isOpaque(), getParent() etc.

//////        // Shouldn't access graphics - no buffering would cause flickering
//////        if (bufferType == BUFFER_NONE) return false;

        // Cannot access graphics - there are some child components
        if (getComponentCount() != 0) return false;

        // Cannot access graphics - component doesn't fully control its area
        if (!isOpaque()) return false;

        // Cannot access graphics - not in Swing tree
        if (!(getParent() instanceof JComponent)) return false;

        // Cannot access graphics - component not showing, doesn't make sense
        if (!isShowing()) return false;

        // Cannot access graphics - component area is not up-to-date
        Rectangle dirtyRegion = RepaintManager.currentManager(this).
                                getDirtyRegion((JComponent)getParent());
        if (dirtyRegion != null && dirtyRegion.width > 0 &&
            dirtyRegion.height > 0) return false;

        // --- Reused from JViewport -------------------------------------------

        Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
        Rectangle oldClip = new Rectangle();
        Rectangle tmp2 = null;
        Container parent;
        Component lastParent = null;
        int x, y, w, h;

        for (parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
            x = parent.getX();
            y = parent.getY();
            w = parent.getWidth();
            h = parent.getHeight();

            oldClip.setBounds(clip);
            SwingUtilities.computeIntersection(0, 0, w, h, clip);
            if (!clip.equals(oldClip)) return false;

            if (lastParent != null && parent instanceof JComponent &&
               !((JComponent)parent).isOptimizedDrawingEnabled()) {
                Component comps[] = parent.getComponents();
                int index = 0;

                for (int i = comps.length - 1 ;i >= 0; i--) {
                    if (comps[i] == lastParent) {
                    index = i - 1;
                    break;
                    }
                }

                while (index >= 0) {
                    tmp2 = comps[index].getBounds(tmp2);
                    if (tmp2.intersects(clip)) return false;
                    index--;
                }
            }
            clip.x += x;
            clip.y += y;
            lastParent = parent;
        }

        // No Window parent.
        if (parent == null) return false;

        return true;
    }