List of usage examples for java.awt Component getWidth
public int getWidth()
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 ww w . j av a 2s .c om*/ 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 a va 2s . co 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 . java2 s .c o m */ 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 ww .j a va 2 s .c om 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
/** * Use this static method if you want to center a component over another * component.// w w w . ja v a 2 s . c o m * * @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
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; }/*from www. j ava2 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
/** * Centers the component on the screen/*w ww . ja v a 2 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 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 v a2 s . c o m*/ return image; }
From source file:Main.java
/** * <p>// w w w . j a v a2s. 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);/*w w w .ja v a 2 s .c o m*/ }