Example usage for java.awt Component getHeight

List of usage examples for java.awt Component getHeight

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Returns the current height 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);//  w  w  w .jav  a  2s.c o  m
}

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 www  . j ava2s.  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

/**
 * Use this static method if you want to center a component in Window.
 *
 * @param component/*from w ww . j av a  2s  .  c om*/
 *            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

/**
 * Returns component content size limited by component border.
 *
 * @param component/* w w w  .j  a  va  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 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);

    /*/* ww w . ja v a  2 s. co  m*/
     * 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;
}

From source file:Main.java

public static final Component getChildAtLine(Container container, Point p, boolean horizontal) {
    if (horizontal) {
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component c = container.getComponent(i);
            if (p.x >= c.getX() && p.x < c.getX() + c.getWidth())
                return c;
        }/*w w  w.  j a v  a2 s .  c  o m*/
    } else {
        for (int i = 0; i < container.getComponentCount(); i++) {
            Component c = container.getComponent(i);
            if (p.y >= c.getY() && p.y < c.getY() + c.getHeight())
                return c;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Position the given component at the center of the given parent component or physical screen.
 * // w  w  w .ja v  a  2 s.c o  m
 * @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

/**
 * Centers a component on its parent.//from  w  w w  .j a v a  2 s.com
 *
 * @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

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:no.geosoft.cc.io.GifEncoder.java

/**
 * Write AWT/Swing component to GIF file.
 * /*from  www  .j a v  a  2 s .c o  m*/
 * @param image  Image to write.
 * @param file   File to erite to.
 */
public static void writeFile(Component component, File file) throws AWTException, IOException {
    Image image = component.createImage(component.getWidth(), component.getHeight());
    Graphics graphics = image.getGraphics();
    component.printAll(graphics);

    GifEncoder.writeFile(image, file);
}