Java Code Examples for java.awt.Component#getSize()

The following examples show how to use java.awt.Component#getSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToolbarSeparator.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void paintIcon(Component destination, Graphics g) {
	Dimension dim = destination.getSize();
	g.setColor(Color.GRAY);
	int x = 0;
	int y = 0;
	int w = dim.width;
	int h = dim.height;
	if (h >= w) { // separator is a vertical line in horizontal toolbar
		h -= 8;
		y = 2;
		x = (w - 2) / 2;
		w = 2;
	} else { // separator is a horizontal line in vertical toolbar
		w -= 8;
		x = 2;
		y = (h - 2) / 2;
		h = 2;
	}
	g.fillRect(x, y, w, h);
}
 
Example 2
Source File: UIUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    if (e.isTemporary()) {
        return;
    }
    Component cmp = e.getComponent();
    if(cmp instanceof JComponent) {
        JViewport vp = getViewport(container);
        if(vp == null) {
            return;
        }
        Rectangle vr = vp.getViewRect();
        Point p = SwingUtilities.convertPoint(cmp.getParent(), cmp.getLocation(), container);
        final Rectangle r = new Rectangle(p, cmp.getSize());
        if(vr.intersects(r)) {
            return; 
        }
        container.scrollRectToVisible(r);
    }
}
 
Example 3
Source File: DrawTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 4
Source File: DrawTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 5
Source File: DrawTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
    Rectangle r = getBounds();
    g.setColor(Color.lightGray);
    g.draw3DRect(0, 0, r.width, r.height, false);

    int n = getComponentCount();
    for (int i = 0; i < n; i++) {
        Component comp = getComponent(i);
        if (comp instanceof Checkbox) {
            Point loc = comp.getLocation();
            Dimension d = comp.getSize();
            g.setColor(comp.getForeground());
            g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
        }
    }
}
 
Example 6
Source File: ScrollAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void scroll( int increment ) {
    Component content = scrollPane.getViewport().getView();
    Dimension size = content.getSize();
    Dimension viewSize = scrollPane.getViewport().getExtentSize();
    Point position = scrollPane.getViewport().getViewPosition();

    if( isHorizontal ) {
        position.x += increment;
        position.x = Math.max( position.x, 0 );
        position.x = Math.min( position.x, size.width-viewSize.width );
    } else {
        position.y += increment;
        position.y = Math.max( position.y, 0 );
        position.y = Math.min( position.y, size.height-viewSize.height );
    }

    scrollPane.getViewport().setViewPosition( position );
}
 
Example 7
Source File: MainDesktopPane.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public void centerJIF(Component comp) {
	Dimension desktopSize = getSize();
	Dimension jInternalFrameSize = comp.getSize();
	int width = (desktopSize.width - jInternalFrameSize.width) / 2;
	int height = (desktopSize.height - jInternalFrameSize.height) / 2;
	comp.setLocation(width, height);
	comp.setVisible(true);
}
 
Example 8
Source File: UIRes.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static Point getPointToCenter(Component component, Dimension dimension) {

		Dimension screenSize = getDefaultDeviceScreenSize();

		if (component == null) {

			if (dimension.height > screenSize.height) {
				dimension.height = screenSize.height;
			}

			if (dimension.width > screenSize.width) {
				dimension.width = screenSize.width;
			}

			return new Point((screenSize.width - dimension.width) / 2, (screenSize.height - dimension.height) / 2);
		}

		Dimension frameDim = component.getSize();
		Rectangle dRec = new Rectangle(component.getX(), component.getY(), (int) frameDim.getWidth(),
				(int) frameDim.getHeight());

		int dialogX = dRec.x + ((dRec.width - dimension.width) / 2);
		int dialogY = dRec.y + ((dRec.height - dimension.height) / 2);

		if (dialogX <= 0 || dialogY <= 0) {

			if (dimension.height > screenSize.height) {
				dimension.height = screenSize.height;
			}

			if (dimension.width > screenSize.width) {
				dimension.width = screenSize.width;
			}

			dialogX = (screenSize.width - dimension.width) / 2;
			dialogY = (screenSize.height - dimension.height) / 2;
		}

		return new Point(dialogX, dialogY);
	}
 
Example 9
Source File: JRobot.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Move mouse cursor to the center of the Component.
 * @param c Component the mouse is placed over
 */
public void moveMouseTo(Component c) {
    Point p = c.getLocationOnScreen();
    Dimension size = c.getSize();
    p.x += size.width / 2;
    p.y += size.height / 2;
    mouseMove(p.x, p.y);
    delay();
}
 
Example 10
Source File: AttackParameterGui.java    From OpenID-Attacker with GNU General Public License v2.0 5 votes vote down vote up
private void formAncestorResized(HierarchyEvent evt) {//GEN-FIRST:event_formAncestorResized
    Component parent = evt.getChangedParent();
    int newHeight = getSize().height;
    int newWidth = parent.getSize().width - 40;
    setSize(newWidth, newHeight);
    setPreferredSize(getSize());
    revalidate();
}
 
Example 11
Source File: BasicBorders.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y,
                        int width, int height) {
    if (!(c instanceof BasicSplitPaneDivider)) {
        return;
    }
    Component          child;
    Rectangle          cBounds;
    JSplitPane         splitPane = ((BasicSplitPaneDivider)c).
                                 getBasicSplitPaneUI().getSplitPane();
    Dimension          size = c.getSize();

    child = splitPane.getLeftComponent();
    // This is needed for the space between the divider and end of
    // splitpane.
    g.setColor(c.getBackground());
    g.drawRect(x, y, width - 1, height - 1);
    if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
        if(child != null) {
            g.setColor(highlight);
            g.drawLine(0, 0, 0, size.height);
        }
        child = splitPane.getRightComponent();
        if(child != null) {
            g.setColor(shadow);
            g.drawLine(size.width - 1, 0, size.width - 1, size.height);
        }
    } else {
        if(child != null) {
            g.setColor(highlight);
            g.drawLine(0, 0, size.width, 0);
        }
        child = splitPane.getRightComponent();
        if(child != null) {
            g.setColor(shadow);
            g.drawLine(0, size.height - 1, size.width,
                       size.height - 1);
        }
    }
}
 
Example 12
Source File: QueryTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void focusGained(FocusEvent e) {
    Component c = e.getComponent();
    if(c instanceof JComponent) {
        Point p = SwingUtilities.convertPoint(c.getParent(), c.getLocation(), repoPanel);
        final Rectangle r = new Rectangle(p, c.getSize());
        UIUtils.runInAWT(new Runnable() {
            @Override
            public void run() {
                repoPanel.scrollRectToVisible(r);
            }
        });
    }
}
 
Example 13
Source File: BasicBorders.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void paintBorder(Component c, Graphics g, int x, int y,
                        int width, int height) {
    if (!(c instanceof BasicSplitPaneDivider)) {
        return;
    }
    Component          child;
    Rectangle          cBounds;
    JSplitPane         splitPane = ((BasicSplitPaneDivider)c).
                                 getBasicSplitPaneUI().getSplitPane();
    Dimension          size = c.getSize();

    child = splitPane.getLeftComponent();
    // This is needed for the space between the divider and end of
    // splitpane.
    g.setColor(c.getBackground());
    g.drawRect(x, y, width - 1, height - 1);
    if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
        if(child != null) {
            g.setColor(highlight);
            g.drawLine(0, 0, 0, size.height);
        }
        child = splitPane.getRightComponent();
        if(child != null) {
            g.setColor(shadow);
            g.drawLine(size.width - 1, 0, size.width - 1, size.height);
        }
    } else {
        if(child != null) {
            g.setColor(highlight);
            g.drawLine(0, 0, size.width, 0);
        }
        child = splitPane.getRightComponent();
        if(child != null) {
            g.setColor(shadow);
            g.drawLine(0, size.height - 1, size.width,
                       size.height - 1);
        }
    }
}
 
Example 14
Source File: NetworkMonitorTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean getScrollableTracksViewportWidth() {
    Component parent = getParent();

    return parent != null ? (getUI().getPreferredSize(this).width <= parent
            .getSize().width) : true;
}
 
Example 15
Source File: Show.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
public static void centrateComponent(Component parent, Component c) {
    if (parent == null) {
        centrateComponent(c);
    } else {
        Dimension dimParent = parent.getSize();
        Point p = parent.getLocation();
        Dimension dimComp = c.getSize();
        c.setLocation(p.x + ((dimParent.width - dimComp.width) / 2),
                p.y + ((dimParent.height - dimComp.height) / 2));
    }
}
 
Example 16
Source File: PatternResourcesSetupPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void resize( Component component, double height )
{
    Dimension size = component.getSize();
    if ( size.height < height ){
        component.setPreferredSize(
                new Dimension(size.width, (int)height));
    }
}
 
Example 17
Source File: BasicScrollPaneUI.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
private void scroll(JScrollPane scrollpane, int orientation,
                    int direction, boolean block) {
    JViewport vp = scrollpane.getViewport();
    Component view;
    if (vp != null && (view = vp.getView()) != null) {
        Rectangle visRect = vp.getViewRect();
        Dimension vSize = view.getSize();
        int amount;

        if (view instanceof Scrollable) {
            if (block) {
                amount = ((Scrollable)view).getScrollableBlockIncrement
                         (visRect, orientation, direction);
            }
            else {
                amount = ((Scrollable)view).getScrollableUnitIncrement
                         (visRect, orientation, direction);
            }
        }
        else {
            if (block) {
                if (orientation == SwingConstants.VERTICAL) {
                    amount = visRect.height;
                }
                else {
                    amount = visRect.width;
                }
            }
            else {
                amount = 10;
            }
        }
        if (orientation == SwingConstants.VERTICAL) {
            visRect.y += (amount * direction);
            if ((visRect.y + visRect.height) > vSize.height) {
                visRect.y = Math.max(0, vSize.height - visRect.height);
            }
            else if (visRect.y < 0) {
                visRect.y = 0;
            }
        }
        else {
            if (scrollpane.getComponentOrientation().isLeftToRight()) {
                visRect.x += (amount * direction);
                if ((visRect.x + visRect.width) > vSize.width) {
                    visRect.x = Math.max(0, vSize.width - visRect.width);
                } else if (visRect.x < 0) {
                    visRect.x = 0;
                }
            } else {
                visRect.x -= (amount * direction);
                if (visRect.width > vSize.width) {
                    visRect.x = vSize.width - visRect.width;
                } else {
                    visRect.x = Math.max(0, Math.min(vSize.width - visRect.width, visRect.x));
                }
            }
        }
        vp.setViewPosition(visRect.getLocation());
    }
}
 
Example 18
Source File: BasicScrollPaneUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void scroll(JScrollPane scrollpane, int orientation,
                    int direction, boolean block) {
    JViewport vp = scrollpane.getViewport();
    Component view;
    if (vp != null && (view = vp.getView()) != null) {
        Rectangle visRect = vp.getViewRect();
        Dimension vSize = view.getSize();
        int amount;

        if (view instanceof Scrollable) {
            if (block) {
                amount = ((Scrollable)view).getScrollableBlockIncrement
                         (visRect, orientation, direction);
            }
            else {
                amount = ((Scrollable)view).getScrollableUnitIncrement
                         (visRect, orientation, direction);
            }
        }
        else {
            if (block) {
                if (orientation == SwingConstants.VERTICAL) {
                    amount = visRect.height;
                }
                else {
                    amount = visRect.width;
                }
            }
            else {
                amount = 10;
            }
        }
        if (orientation == SwingConstants.VERTICAL) {
            visRect.y += (amount * direction);
            if ((visRect.y + visRect.height) > vSize.height) {
                visRect.y = Math.max(0, vSize.height - visRect.height);
            }
            else if (visRect.y < 0) {
                visRect.y = 0;
            }
        }
        else {
            if (scrollpane.getComponentOrientation().isLeftToRight()) {
                visRect.x += (amount * direction);
                if ((visRect.x + visRect.width) > vSize.width) {
                    visRect.x = Math.max(0, vSize.width - visRect.width);
                } else if (visRect.x < 0) {
                    visRect.x = 0;
                }
            } else {
                visRect.x -= (amount * direction);
                if (visRect.width > vSize.width) {
                    visRect.x = vSize.width - visRect.width;
                } else {
                    visRect.x = Math.max(0, Math.min(vSize.width - visRect.width, visRect.x));
                }
            }
        }
        vp.setViewPosition(visRect.getLocation());
    }
}
 
Example 19
Source File: BasicScrollPaneUI.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void scroll(JScrollPane scrollpane, int orientation,
                    int direction, boolean block) {
    JViewport vp = scrollpane.getViewport();
    Component view;
    if (vp != null && (view = vp.getView()) != null) {
        Rectangle visRect = vp.getViewRect();
        Dimension vSize = view.getSize();
        int amount;

        if (view instanceof Scrollable) {
            if (block) {
                amount = ((Scrollable)view).getScrollableBlockIncrement
                         (visRect, orientation, direction);
            }
            else {
                amount = ((Scrollable)view).getScrollableUnitIncrement
                         (visRect, orientation, direction);
            }
        }
        else {
            if (block) {
                if (orientation == SwingConstants.VERTICAL) {
                    amount = visRect.height;
                }
                else {
                    amount = visRect.width;
                }
            }
            else {
                amount = 10;
            }
        }
        if (orientation == SwingConstants.VERTICAL) {
            visRect.y += (amount * direction);
            if ((visRect.y + visRect.height) > vSize.height) {
                visRect.y = Math.max(0, vSize.height - visRect.height);
            }
            else if (visRect.y < 0) {
                visRect.y = 0;
            }
        }
        else {
            if (scrollpane.getComponentOrientation().isLeftToRight()) {
                visRect.x += (amount * direction);
                if ((visRect.x + visRect.width) > vSize.width) {
                    visRect.x = Math.max(0, vSize.width - visRect.width);
                } else if (visRect.x < 0) {
                    visRect.x = 0;
                }
            } else {
                visRect.x -= (amount * direction);
                if (visRect.width > vSize.width) {
                    visRect.x = vSize.width - visRect.width;
                } else {
                    visRect.x = Math.max(0, Math.min(vSize.width - visRect.width, visRect.x));
                }
            }
        }
        vp.setViewPosition(visRect.getLocation());
    }
}
 
Example 20
Source File: BasicScrollPaneUI.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void scroll(JScrollPane scrollpane, int orientation,
                    int direction, boolean block) {
    JViewport vp = scrollpane.getViewport();
    Component view;
    if (vp != null && (view = vp.getView()) != null) {
        Rectangle visRect = vp.getViewRect();
        Dimension vSize = view.getSize();
        int amount;

        if (view instanceof Scrollable) {
            if (block) {
                amount = ((Scrollable)view).getScrollableBlockIncrement
                         (visRect, orientation, direction);
            }
            else {
                amount = ((Scrollable)view).getScrollableUnitIncrement
                         (visRect, orientation, direction);
            }
        }
        else {
            if (block) {
                if (orientation == SwingConstants.VERTICAL) {
                    amount = visRect.height;
                }
                else {
                    amount = visRect.width;
                }
            }
            else {
                amount = 10;
            }
        }
        if (orientation == SwingConstants.VERTICAL) {
            visRect.y += (amount * direction);
            if ((visRect.y + visRect.height) > vSize.height) {
                visRect.y = Math.max(0, vSize.height - visRect.height);
            }
            else if (visRect.y < 0) {
                visRect.y = 0;
            }
        }
        else {
            if (scrollpane.getComponentOrientation().isLeftToRight()) {
                visRect.x += (amount * direction);
                if ((visRect.x + visRect.width) > vSize.width) {
                    visRect.x = Math.max(0, vSize.width - visRect.width);
                } else if (visRect.x < 0) {
                    visRect.x = 0;
                }
            } else {
                visRect.x -= (amount * direction);
                if (visRect.width > vSize.width) {
                    visRect.x = vSize.width - visRect.width;
                } else {
                    visRect.x = Math.max(0, Math.min(vSize.width - visRect.width, visRect.x));
                }
            }
        }
        vp.setViewPosition(visRect.getLocation());
    }
}