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 center(Component c) {
    Dimension screenSize = c.getToolkit().getScreenSize();
    Dimension componentSize = c.getSize();
    int xPos = (screenSize.width - componentSize.width) / 2;
    xPos = Math.max(xPos, 0);//w w w.  j a  v a2  s  .co m
    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 constrainResize(ComponentEvent componentevent) {
    Component component = componentevent.getComponent();
    Dimension dimension = component.getSize();
    Dimension dimension1 = component.getMinimumSize();
    Dimension dimension2 = new Dimension(dimension);
    boolean flag = false;
    if (dimension.width < dimension1.width) {
        flag = true;//from w ww. ja va  2  s.  c  o  m
        dimension2.width = dimension1.width;
    }
    if (dimension.height < dimension1.height) {
        flag = true;
        dimension2.height = dimension1.height;
    }
    if (flag)
        component.setSize(dimension2);
}

From source file:Main.java

static public void alignComponents(Component child, Component parent) {
    Point location = parent.getLocation();
    Dimension parentSize = parent.getSize();
    Dimension childSize = child.getSize();

    if ((location.x - childSize.width) > 0) {
        location.x -= childSize.width;/*from   ww  w  .ja  v  a2 s.c  o m*/
        child.setLocation(location);
    } else if ((location.x + parentSize.width + childSize.width) < SCREEN_SIZE.width) {
        location.x += parentSize.width;
        child.setLocation(location);
    } else
        centerComponent(child, location, parentSize);
}

From source file:Main.java

public static BufferedImage takeScreenShot(Component component, String... watermarks) {

    Dimension size = component.getSize();

    BufferedImage screenshot = new BufferedImage(size.width, size.height, Transparency.OPAQUE);
    Graphics2D g = screenshot.createGraphics();
    g.setClip(0, 0, size.width - 1, size.height - 1);

    component.update(g);/*from  w  w w.  j  av  a2 s  .  c om*/

    FontMetrics fm = g.getFontMetrics();
    int y = fm.getDescent();
    for (String watermark : watermarks)
        if (watermark != null) {
            int x = size.width - SwingUtilities.computeStringWidth(fm, watermark);

            g.setColor(Color.black);
            g.drawString(watermark, x, y);

            g.setColor(Color.white);
            g.drawString(watermark, x - 1, y - 1);

            y -= fm.getHeight();
        }

    g.dispose();

    return screenshot;
}

From source file:Main.java

/**
 * Centers a component according to the window location.
 * @param wnd The parent window//  w  w w  . j  ava 2 s.  c  om
 * @param cmp A component, usually a dialog
 */
public static void centerInWindow(Window wnd, Component cmp) {
    Dimension size = wnd.getSize();
    Point loc = wnd.getLocationOnScreen();
    Dimension cmpSize = cmp.getSize();
    loc.x += (size.width - cmpSize.width) / 2;
    loc.y += (size.height - cmpSize.height) / 2;
    cmp.setBounds(loc.x, loc.y, cmpSize.width, cmpSize.height);
}

From source file:Main.java

public static final void center(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = component.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }// w w  w .  j  a va  2s. co m

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    component.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:Main.java

public static void moveToScreenCenter(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension componentSize = component.getSize();
    int newComponentX = screenSize.width - componentSize.width;
    if (newComponentX >= 0)
        newComponentX = newComponentX / 2;
    else/* w  w w  .  j a  v  a  2  s  .c o m*/
        newComponentX = 0;
    int newComponentY = screenSize.height - componentSize.height;
    if (newComponentY >= 0)
        newComponentY = newComponentY / 2;
    else
        newComponentY = 0;
    component.setLocation(newComponentX, newComponentY);
}

From source file:Main.java

public static final void fullScreen(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = component.getSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }/*from   ww w .j  av a  2  s. c  om*/

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    component.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}

From source file:Main.java

public static Point moveToScreenCenter(Component component) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension componentSize = component.getSize();
    int newComponentX = screenSize.width - componentSize.width;
    if (newComponentX >= 0)
        newComponentX = newComponentX / 2;
    else// w  ww .j  a  v a 2 s  . co m
        newComponentX = 0;
    int newComponentY = screenSize.height - componentSize.height;
    if (newComponentY >= 0)
        newComponentY = newComponentY / 2;
    else
        newComponentY = 0;
    Point location = new Point(newComponentX, newComponentY);
    component.setLocation(location);
    return location;
}

From source file:Main.java

/**
 * Returns component bounds relative to another component.
 *
 * @param component//from  ww  w  .  j  a  v a2s . c o  m
 *            component to process
 * @param relativeTo
 *            component relative to which bounds will be returned
 * @return component bounds relative to another component
 */
public static Rectangle getRelativeBounds(final Component component, final Component relativeTo) {
    return new Rectangle(getRelativeLocation(component, relativeTo), component.getSize());
}