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

/**
 * This main() method demonstrates the use of the ComponentTree class: it
 * puts a ComponentTree component in a Frame, and uses the ComponentTree to
 * display its own GUI hierarchy. It also adds a TreeSelectionListener to
 * display additional information about each component as it is selected
 *///from w w  w .j  a v a  2  s . co  m
public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            // Tree selections are referred to by "path"
            // We only care about the last node in the path
            TreePath path = e.getPath();
            Component c = (Component) path.getLastPathComponent();
            // Now we know what component was selected, so
            // display some information about it in the message line
            if (c.isShowing()) {
                Point p = c.getLocationOnScreen();
                msgline.setText("x: " + p.x + "  y: " + p.y + "  width: " + c.getWidth() + "  height: "
                        + c.getHeight());
            } else {
                msgline.setText("component is not showing");
            }
        }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static Rectangle getRelativeBounds(Component component) {
    Rectangle bounds = new Rectangle(0, 0, component.getWidth(), component.getHeight());
    Container parent = component.getParent();

    while (parent != null) {
        bounds.x += component.getX();//from  w  ww . j ava  2  s.  c o  m
        bounds.y += component.getY();
        component = parent;
        parent = component.getParent();
    }

    return bounds;
}

From source file:Main.java

/**
 * Returns component size represented as a rectangle with zero X and Y coordinates.
 *
 * @param component component to process
 * @return component size rectangle/*  w ww. j  av a  2  s.  c om*/
 */
public static Rectangle size(final Component component) {
    return new Rectangle(0, 0, component.getWidth(), component.getHeight());
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

public static void generatePDF(Component component, File file) {
    int width = component.getWidth();
    int height = component.getHeight();
    try {/*from w  w w .j  av a2s .com*/
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        component.paint(g2);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static BufferedImage createImage(Component comp) {
    if (comp == null)
        return null;

    BufferedImage image = (BufferedImage) comp.createImage(comp.getWidth(), comp.getHeight());
    Graphics g = image.createGraphics();
    comp.paintAll(g);//  ww  w .j  a va  2 s  .  c om
    return image;
}

From source file:Main.java

/**
 * Centers the component on the screen//from w  w w  .ja  va2 s  .  c om
 */
public static void centerOnScreen(Component comp) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    comp.setLocation((screenSize.width - comp.getWidth()) / 2, (screenSize.height - comp.getHeight()) / 2);
}

From source file:Main.java

public static int getHeight(final Component component) {
    if (component != null) {
        if (SwingUtilities.isEventDispatchThread()) {
            return component.getHeight();
        } else {//from  w  w w .j  av a2 s  .com
            final int[] height = new int[1];

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

            }

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

From source file:Main.java

/**
 * Use this static method if you want to center a component over another
 * component.//w  w w . j a v a  2  s . c om
 *
 * @param parent
 *            the component you want to use to place it on
 * @param toBeCentered
 *            the component you want to center
 */
public static void centerComponentInComponent(Component parent, Component toBeCentered) {
    toBeCentered.setLocation(parent.getX() + (parent.getWidth() - toBeCentered.getWidth()) / 2,
            parent.getY() + (parent.getHeight() - toBeCentered.getHeight()) / 2);

    toBeCentered.validate();
    toBeCentered.repaint();
}

From source file:Main.java

/**
 * <p>//from ww  w .j  a v  a  2s .  c o m
 * getCenter.
 * </p>
 *
 * @param component
 *            a {@link java.awt.Component} object.
 * @return a {@link java.awt.Point} object.
 */
public static Point getCenter(final Component component) {
    final Point point = component.getLocation();
    point.translate( //
            (int) Math.floor((component.getWidth() + 0.5) / 2.0), //
            (int) Math.floor((component.getHeight() + 0.5) / 2.0));
    return point;
}

From source file:Main.java

public static void center(final Component c) {
    final Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenDimensions.width - c.getWidth()) / 2;
    int y = (screenDimensions.height - c.getHeight()) / 2;
    c.setLocation(x, y);/*from   www  .j av  a2 s. c  o m*/
}