Example usage for java.awt Component getSize

List of usage examples for java.awt Component getSize

Introduction

In this page you can find the example usage for java.awt Component getSize.

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:Main.java

public static void centerInParent(Window window, Component myParent) {
    int x;//from   w  w  w  . ja  v a 2 s  . co  m
    int y;

    Point topLeft = myParent.getLocationOnScreen();
    Dimension parentSize = myParent.getSize();
    Dimension mySize = window.getSize();
    if (parentSize.width > mySize.width)
        x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
    else
        x = topLeft.x;

    if (parentSize.height > mySize.height)
        y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
    else
        y = topLeft.y;

    window.setLocation(x, y);
}

From source file:Main.java

private static void center(final Component wind, final Rectangle rect) {
    if ((wind == null) || (rect == null)) {
        return;//www.  jav a 2 s  . c  o  m
    }
    final Dimension windSize = wind.getSize();
    final int x = ((rect.width - windSize.width) / 2) + rect.x;
    int y = ((rect.height - windSize.height) / 2) + rect.y;
    if (y < rect.y) {
        y = rect.y;
    }
    wind.setLocation(x, y);
}

From source file:Main.java

public static void center(Component c) {
    Dimension screenSize = c.getToolkit().getScreenSize();
    screenSize.width -= BORDER_SIZE;//from  w  w w .j a  va2  s .com
    screenSize.height -= BORDER_SIZE;
    Dimension componentSize = c.getSize();
    int xPos = (screenSize.width - componentSize.width) / 2;
    xPos = Math.max(xPos, 0);
    int yPos = (screenSize.height - componentSize.height) / 2;
    yPos = Math.max(yPos, 0);
    c.setLocation(new Point(xPos, yPos));
}

From source file:Main.java

public static void setCenter(Component component, Component component1) {
    Point point;// w  ww  .j a v  a  2  s.  co m
    Dimension dimension;
    if (component != null) {
        point = component.getLocation();
        dimension = component.getSize();
    } else {
        dimension = Toolkit.getDefaultToolkit().getScreenSize();
        point = new Point(0, 0);
    }
    Dimension dimension1 = Toolkit.getDefaultToolkit().getScreenSize();
    point.setLocation((double) point.x + dimension.getWidth() / 2D,
            (double) point.y + dimension.getHeight() / 2D);
    Point point1 = new Point(point.x - component1.getWidth() / 2, point.y - component1.getHeight() / 2);
    if (point1.y < 0)
        point1.y = 0;
    if (point1.x < 0)
        point1.y = 0;
    if (point1.x + component1.getWidth() > dimension1.width)
        point1.x = dimension1.width - component1.getWidth();
    if (point1.y + component1.getHeight() > dimension1.height)
        point1.y = dimension1.height - component1.getHeight();
    component1.setLocation(point1);
}

From source file:GUIUtils.java

public static boolean isWithinParent(Component wind) {
    if (wind == null) {
        throw new IllegalArgumentException("Null Component passed");
    }//from   w ww .j a v a 2 s.  c o  m

    Rectangle windowBounds = wind.getBounds();
    Component parent = wind.getParent();
    Rectangle parentRect = null;
    if (parent != null) {
        parentRect = new Rectangle(parent.getSize());
    } else {
        // parentRect = new
        // Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        parentRect = getScreenBoundsFor(windowBounds);
    }

    // if (windowBounds.x > (parentRect.width - 20)
    // || windowBounds.y > (parentRect.height - 20)
    // || (windowBounds.x + windowBounds.width) < 20
    // || (windowBounds.y + windowBounds.height) < 20)
    // {
    // return false;
    // }
    if (windowBounds.x < (parentRect.x - 20) || windowBounds.y < (parentRect.y - 20)) {
        return false;
    }
    return true;
}

From source file:Win.java

public static void position(Window frame, Component ref, double xfrac, double yfrac) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = frame.getSize();
    Dimension refSize = (ref != null) ? ref.getSize() : screenSize;
    Point origin = (ref != null) ? ref.getLocationOnScreen() : new Point(0, 0);

    int x = origin.x + relativePoint(xfrac, refSize.width, size.width);
    int y = origin.y + relativePoint(yfrac, refSize.height, size.height);

    // make sure frame is entirely on screen
    x = Math.max(0, Math.min(screenSize.width - size.width, x));
    y = Math.max(0, Math.min(screenSize.height - size.height, y));

    frame.setLocation(x, y);/*from  www .j  a va  2  s.c o m*/
}

From source file:Main.java

/**
 * Centers the given component over another component.
 * <p/>//ww  w . ja v a  2 s.c  o m
 * <p> The method performs the alignment by setting a newly computed location for the component. It does not alter
 * the component's size.
 *
 * @param comp      the component whose location is to be altered
 * @param alignComp the component used for the alignment of the first component, if <code>null</code> the component
 *                  is ceneterd within the screen area
 *
 * @throws IllegalArgumentException if the component is <code>null</code>
 */
public static void centerComponent(Component comp, Component alignComp) {

    if (comp == null) {
        throw new IllegalArgumentException("comp must not be null");
    }

    Dimension compSize = comp.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int x1, y1;

    if (alignComp != null && !new Rectangle(alignComp.getSize()).isEmpty()) {
        Point alignCompOffs = alignComp.getLocation();
        Dimension alignCompSize = alignComp.getSize();
        x1 = alignCompOffs.x + (alignCompSize.width - compSize.width) / 2;
        y1 = alignCompOffs.y + (alignCompSize.height - compSize.height) / 2;
    } else {
        x1 = (screenSize.width - compSize.width) / 2;
        y1 = (screenSize.height - compSize.height) / 2;
    }

    int x2 = x1 + compSize.width;
    int y2 = y1 + compSize.height;

    if (x2 >= screenSize.width) {
        x1 = screenSize.width - compSize.width - 1;
    }
    if (y2 >= screenSize.height) {
        y1 = screenSize.height - compSize.height - 1;
    }
    if (x1 < 0) {
        x1 = 0;
    }
    if (y1 < 0) {
        y1 = 0;
    }

    comp.setLocation(x1, y1);
}

From source file:Main.java

/**
 * Gauge the given component on screen./*from w w  w  .  ja  v a 2  s.  c  o  m*/
 *
 * @param component Swing-component to gauge.
 */
public static void makeCentered(final Component component) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dim = toolkit.getScreenSize();
    int screenWidth = dim.width;
    int screenHeight = dim.height;
    component.setLocation((screenWidth - component.getSize().width) / 2,
            (screenHeight - component.getSize().height) / 2);
}

From source file:BooksDemo.java

public static void centerOnScreen(Component component) {
    Dimension paneSize = component.getSize();
    Dimension screenSize = component.getToolkit().getScreenSize();
    component.setLocation((screenSize.width - paneSize.width) / 2, (screenSize.height - paneSize.height) / 2);
}

From source file:Main.java

/**
 * Centers a component on screen./*w ww.  j a v a 2  s  .c o m*/
 *
 * @param c The component that will be centered.
 */
public static void centerComponentOnScreen(Component c) {

    // Size of the screen.
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    double width = screenSize.getWidth();
    double height = screenSize.getHeight();

    // Size of the component.
    Dimension size = c.getSize();

    width -= size.getWidth();
    height -= size.getHeight();

    c.setLocation((int) width / 2, (int) height / 2);
}