Example usage for java.awt Dimension Dimension

List of usage examples for java.awt Dimension Dimension

Introduction

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

Prototype

public Dimension(int width, int height) 

Source Link

Document

Constructs a Dimension and initializes it to the specified width and specified height.

Usage

From source file:Main.java

public static void setJButtonSizesTheSame(final JButton[] btns) {
    if (btns == null) {
        throw new IllegalArgumentException();
    }/*www . jav a2  s. com*/

    final Dimension maxSize = new Dimension(0, 0);
    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        final FontMetrics fm = btn.getFontMetrics(btn.getFont());
        final Rectangle2D bounds = fm.getStringBounds(btn.getText(), btn.getGraphics());
        final int boundsHeight = (int) bounds.getHeight();
        final int boundsWidth = (int) bounds.getWidth();
        maxSize.width = boundsWidth > maxSize.width ? boundsWidth : maxSize.width;
        maxSize.height = boundsHeight > maxSize.height ? boundsHeight : maxSize.height;
    }

    final Insets insets = btns[0].getInsets();
    maxSize.width += insets.left + insets.right;
    maxSize.height += insets.top + insets.bottom;

    for (int i = 0; i < btns.length; ++i) {
        final JButton btn = btns[i];
        btn.setPreferredSize(maxSize);
    }

    initComponentHeight(btns);
}

From source file:Main.java

/** Returns the Screen resolution of all available displays. If ther's only one display you might use: {@link #getScreenSize()}
 * @return Screen resolution of all available displays.
 * @see #getScreenSize()/*from w  w  w .j  a va2  s.  c  o  m*/
 */
public static Dimension[] getScreenSizes() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices(); // Get size of each screen
    Dimension[] dimension = new Dimension[gs.length];
    for (int i = 0; i < gs.length; i++) {
        DisplayMode dm = gs[i].getDisplayMode();
        dimension[i] = new Dimension(dm.getWidth(), dm.getHeight());
    }
    return dimension;
}

From source file:Main.java

/**
 * @param mainView/*from  w  ww . j av a2 s.  co m*/
 * @param preferredWidth Frame preferred width.
 * @param preferredHeight Frame preferred height.
 */
public static void instanceDefaults(JFrame mainView, int preferredWidth, int preferredHeight) {
    mainView.getContentPane().setPreferredSize(new Dimension(preferredWidth, preferredHeight));
    instanceDefaults(mainView);
}

From source file:Main.java

public static Dimension expandSize(Dimension d, Insets insets) {
    if (insets != null) {
        d = new Dimension(d.width + insets.left + insets.right, d.height + insets.top + insets.bottom);
    }//from   ww  w  . ja v a2s .  c o m
    return d;
}

From source file:Main.java

public static void makeButtonsSameSize(JButton[] buttons) {

    if (buttons.length < 2) {
        return; // nothing to do
    }/*from  ww  w  .  ja  v a 2s.co  m*/

    Dimension size = buttons[0].getPreferredSize();
    int maxWidth = size.width;
    int maxHeight = size.height;
    int i;

    for (i = 1; i < buttons.length; i++) {
        size = buttons[i].getPreferredSize();
        if (size.width > maxWidth) {
            maxWidth = size.width;
        }
        if (size.height > maxHeight) {
            maxHeight = size.height;
        }
    }

    size = new Dimension(maxWidth, maxHeight);

    for (i = 0; i < buttons.length; i++) {
        buttons[i].setPreferredSize(size);
        buttons[i].setMinimumSize(size);
        buttons[i].setMaximumSize(size);
    }

}

From source file:Main.java

public static Dimension getTextDimension(final String text, final Font font) {
    if (defaultLabel == null) {
        defaultLabel = new JLabel();
    }//from w w w  .  j ava2s.  c  o  m

    // get metrics from the graphics
    FontMetrics fontMetrics = defaultLabel.getFontMetrics(font);

    // get the height of a line of text in this
    // font and render context
    int hgt = fontMetrics.getHeight();
    // get the advance of my text in this font
    // and render context
    int adv = fontMetrics.stringWidth(text);
    // calculate the size of a box to hold the text
    Dimension size = new Dimension(adv, hgt);

    return size;
}

From source file:Main.java

public static void removeFixedSize(Component c) {
    c.setMinimumSize(new Dimension(0, 0));
    c.setMaximumSize(new Dimension(65535, 65535));
}

From source file:Main.java

/**
 * Gets the max window size./*from  w w w .j a  v  a  2  s . c  o m*/
 *
 * @return the max window size
 */
public static Dimension getMaxWindowSize() {
    final Dimension d = getScreenDimesion();
    return new Dimension((int) d.getWidth() - 100, (int) d.getHeight() - 100);
}

From source file:Main.java

/**
 * Freezes the width of the given component to the given size, preventing
 * it from expanding to fill any additional space.
 * @param component the component to freeze
 * @param width the width to freeze the component to
 * @return the component passed in, for chaining purposes
 */// w  w  w .j  av  a2  s  .c  o  m
public static JComponent freezeWidth(JComponent component, int width) {
    component.setMinimumSize(new Dimension(width, component.getMinimumSize().height));
    component.setPreferredSize(new Dimension(width, component.getPreferredSize().height));
    component.setMaximumSize(new Dimension(width, component.getMaximumSize().height));
    return component;
}

From source file:Main.java

public static void setMaxPreferredSize(JComponent comp) {
    comp.setPreferredSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
}