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:UsingComponentListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JCheckBox checkbox = new JCheckBox("Label visible", true);
    checkbox.addComponentListener(new ComponentListener() {
        public void componentHidden(ComponentEvent e) {
            System.out.println("componentHidden event from " + e.getComponent().getClass().getName());
        }//from  w w  w .ja va 2s  . c o m

        public void componentMoved(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentMoved event from " + c.getClass().getName() + "; new location: "
                    + c.getLocation().x + ", " + c.getLocation().y);
        }

        public void componentResized(ComponentEvent e) {
            Component c = e.getComponent();
            System.out.println("componentResized event from " + c.getClass().getName() + "; new size: "
                    + c.getSize().width + ", " + c.getSize().height);
        }

        public void componentShown(ComponentEvent e) {
            System.out.println("componentShown event from " + e.getComponent().getClass().getName());
        }

    });

    frame.add(checkbox, "North");

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Centers component <code>b</code> within component <code>a</code>.
 *//* w w w .j  a v a 2  s. co m*/
public static void centerComponent(Component a, Component b) {
    Dimension asize = a.getSize(), bsize = b.getSize();
    b.setLocation((asize.width - bsize.width) / 2, (asize.height - bsize.height) / 2);
}

From source file:Main.java

public static void centerOnScreen(Component aCompo) {
    final Dimension lComponentSize = aCompo.getSize();
    centerOnScreen(aCompo, lComponentSize);
}

From source file:Main.java

public static void getComponentRect(Point locationOnScreen, Component component, Rectangle resultStorage) {
    Dimension size = component.getSize();
    resultStorage.setRect(locationOnScreen.x, locationOnScreen.y, size.width, size.height);
}

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;//from w  ww  .j a va  2s .  c om
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}

From source file:Main.java

public static Point getPopupPoint(Component parent, Component comp) {
    Dimension pSize = parent.getSize();
    Point loc = parent.getLocationOnScreen();

    loc.y += pSize.height;//w w  w  .j a  va  2  s  .co m

    return loc;
}

From source file:Main.java

public static Point getCenterPoint(Component parent, Component comp) {
    Dimension cSize = comp.getSize();
    Dimension pSize = parent.getSize();
    Point loc = parent.getLocationOnScreen();

    loc.x = ((pSize.width - cSize.width) / 2) + loc.x;
    loc.y = ((pSize.height - cSize.height) / 2) + loc.y;

    if (loc.x < 0) {
        loc.x = 0;/*from  w  w w. j  ava2 s.c o m*/
    }

    if (loc.y < 0) {
        loc.y = 0;
    }

    return loc;
}

From source file:Main.java

static public Point fitComponentInsideScreen(Component component, Point location) {
    return (fitComponentInsideScreen(component.getSize(), location));
}

From source file:Main.java

/**
 * Returns component bounds on screen./*from w  w w  . j av a  2  s  .c om*/
 *
 * @param component
 *            component to process
 * @return component bounds on screen
 */
public static Rectangle getBoundsOnScreen(final Component component) {
    return new Rectangle(component.getLocationOnScreen(), component.getSize());
}

From source file:Main.java

static public void centerComponent(Component child, Point parentLocation, Dimension parentSize) {
    Dimension childSize = child.getSize();

    if (childSize.width > parentSize.width)
        childSize.width = parentSize.width;
    if (childSize.height > parentSize.height)
        childSize.height = parentSize.height;
    child.setLocation(parentLocation.x + parentSize.width / 2 - childSize.width / 2,
            parentLocation.y + parentSize.height / 2 - childSize.height / 2);
}