Example usage for java.awt Component getWidth

List of usage examples for java.awt Component getWidth

Introduction

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

Prototype

public int getWidth() 

Source Link

Document

Returns the current width of this component.

Usage

From source file:Main.java

public static void center(Component component) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    int x = (screenSize.width - component.getWidth()) / 2;
    int y = (screenSize.height - component.getHeight()) / 2;
    component.setLocation(x, y);/*from   w ww.j  a  v  a 2s  .c o  m*/
}

From source file:Main.java

public static int getWidth(final Component component) {
    if (component != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return component.getWidth();
        } else {//from   ww w .  jav  a2 s.c om
            final int[] width = new int[1];

            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        width[0] = component.getWidth();
                    }
                });
            } catch (InterruptedException | InvocationTargetException e) {

            }

            return width[0];
        }
    } else {
        return 0;
    }
}

From source file:Main.java

/**
 * Use this static method if you want to center a component in Window.
 *
 * @param component// w w  w. ja v a  2s  . co  m
 *            the component you want to center in window
 */
public static void centerComponentInWindow(Component component) {
    Dimension dimension = component.getToolkit().getScreenSize();

    component.setLocation((int) ((dimension.getWidth() - component.getWidth()) / 2),
            (int) ((dimension.getHeight() - component.getHeight()) / 2));
    component.validate();
    component.repaint();
}

From source file:Main.java

public static int getWidth(final Component component) {
    return component != null ? runInEDT(() -> Integer.valueOf(component.getWidth())).intValue() : 0;
}

From source file:Main.java

public static Point getPositionWithinWindow(Component component, Component parent, Point p) {
    Point[] pointCheck = new Point[] { (Point) p.clone(), (Point) p.clone(), (Point) p.clone(),
            (Point) p.clone() };//from  ww w . j a  v  a  2  s.  co m
    int w = component.getWidth();
    int h = component.getHeight();
    pointCheck[0].translate(w, h);
    pointCheck[1].translate(0, h);
    pointCheck[2].translate(w, 0);
    pointCheck[3].translate(0, 0);
    for (Point p2 : pointCheck) {
        if (parent.getBounds().contains(p2)) {
            p2.translate(-w, -h);
            return p2;
        }
    }
    return p;
}

From source file:Main.java

/**
 * Returns component content size limited by component border.
 *
 * @param component/*from  w w  w.  ja  v  a 2  s . c om*/
 *            component to process
 * @return component content size rectangle
 */
public static Rectangle contentSize(final Component component) {
    if (component instanceof JComponent) {
        final Insets i = ((JComponent) component).getInsets();
        return new Rectangle(i.left, i.top, component.getWidth() - i.left - i.right,
                component.getHeight() - i.top - i.bottom);
    } else {
        return size(component);
    }
}

From source file:Main.java

public static boolean isMouseOver(Component component) {
    return MouseInfo.getPointerInfo().getLocation().x >= component.getLocationOnScreen().x
            && MouseInfo.getPointerInfo().getLocation().x <= component.getLocationOnScreen().x
                    + component.getWidth()
            && MouseInfo.getPointerInfo().getLocation().y >= component.getLocationOnScreen().y
            && MouseInfo.getPointerInfo().getLocation().y <= component.getLocationOnScreen().y
                    + component.getHeight();
}

From source file:Main.java

/**
 * Centers a component on its parent.//from   ww w.jav  a 2s.  c om
 *
 * @param component
 */
public static void centerInParent(Component component) {
    Container parent = component.getParent();
    if (parent != null) {
        Rectangle parentBounds = parent.getBounds();
        Rectangle dialogBounds = new Rectangle(
                (int) (parentBounds.getMinX() + parentBounds.getWidth() / 2 - component.getWidth() / 2),
                (int) (parentBounds.getMinY() + parentBounds.getHeight() / 2 - component.getHeight() / 2),
                component.getWidth(), component.getHeight());
        //dialog.setBounds( dialogBounds );
        component.setLocation(dialogBounds.x, dialogBounds.y);
    }
}

From source file:Main.java

/**
 * Position the given component at the center of the given parent component or physical screen.
 * /*from   w  w  w.  j av a2s.  c  om*/
 * @param c the component to be positioned
 * @param parent the component whose center will match the center of the given component.
 * If null, the given component will match the screen center.
 * 
 */
public static void position(Component c, Component parent) {
    Dimension d = c.getPreferredSize();
    if (parent == null) {
        Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
        c.setLocation(s.width / 2 - d.width / 2, s.height / 2 - d.height / 2);
    } else {
        Point p = parent.getLocationOnScreen();
        int pw = parent.getWidth();
        int ph = parent.getHeight();
        c.setLocation(p.x + pw / 2 - d.width / 2, p.y + ph / 2 - d.height / 2);
    }
}

From source file:Main.java

public static Image getImage(Component c) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    // Create an image that supports transparent pixels
    BufferedImage bImage = gc.createCompatibleImage(c.getWidth(), c.getHeight(), Transparency.BITMASK);

    /*/* w  ww  .  ja v  a2s  . c om*/
     * And now this is how we get an image of the component
     */
    Graphics2D g = bImage.createGraphics();

    // Then use the current component we're in and call paint on this
    // graphics object
    c.paint(g);
    return bImage;
}