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

The following examples show how to use java.awt.Rectangle#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: ComboBoxUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
private Point getPopupLocation() {
	Dimension popupSize = new Dimension((int) this.comboBox.getSize().getWidth(), (int) this.comboBox.getSize()
			.getHeight());
	Insets insets = getInsets();
	popupSize.setSize(popupSize.width - (insets.right + insets.left),
			getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
	Rectangle popupBounds = computePopupBounds(0, this.comboBox.getBounds().height, popupSize.width,
			popupSize.height);
	Dimension scrollSize = popupBounds.getSize();
	Point popupLocation = popupBounds.getLocation();

	this.scroller.setMaximumSize(scrollSize);
	this.scroller.setPreferredSize(scrollSize);
	this.scroller.setMinimumSize(scrollSize);

	this.list.revalidate();
	return popupLocation;
}
 
Example 2
Source File: SeaGlassComboPopup.java    From seaglass with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates the upper left location of the Popup.
 *
 * @return the Point representing the upper-left coordinate of the Popup.
 */
private Point getPopupLocation() {
    Dimension popupSize = comboBox.getSize();
    Insets    insets    = getInsets();

    // reduce the width of the scrollpane by the insets so that the popup
    // is the same width as the combo box.
    popupSize.setSize(popupSize.width - (insets.right + insets.left), getPopupHeightForRowCount(getMaximumRowCount()));
    Rectangle popupBounds   = computePopupBounds(0, comboBox.getBounds().height, popupSize.width, popupSize.height);
    Dimension scrollSize    = popupBounds.getSize();
    Point     popupLocation = popupBounds.getLocation();

    scroller.setMaximumSize(scrollSize);
    scroller.setPreferredSize(scrollSize);
    scroller.setMinimumSize(scrollSize);

    list.revalidate();

    return popupLocation;
}
 
Example 3
Source File: BasicGraphicsUtils.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 4
Source File: XlibUtil.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 5
Source File: XlibUtil.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 6
Source File: XlibUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 7
Source File: XlibUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 8
Source File: BasicGraphicsUtils.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 9
Source File: CompletionLayoutPopup.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Create and display the popup at the given bounds.
 *
 * @param popupBounds location and size of the popup.
 * @param displayAboveCaret whether the popup is displayed above the anchor
 *  bounds or below them (it does not be right above them).
 */
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
    // Hide the original popup if exists
    if (popup != null) {
        popup.hide();
        popup = null;
    }
    
    // Explicitly set the preferred size
    Dimension origPrefSize = getPreferredSize();
    Dimension newPrefSize = popupBounds.getSize();
    JComponent contComp = getContentComponent();
    if (contComp == null){
        return;
    }
    contComp.setPreferredSize(newPrefSize);
    showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
    
    PopupFactory factory = PopupFactory.getSharedInstance();
    // Lightweight completion popups don't work well on the Mac - trying
    // to click on its scrollbars etc. will cause the window to be hidden,
    // so force a heavyweight parent by passing in owner==null. (#96717)
    
    JTextComponent owner = layout.getEditorComponent();
    if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) {
        owner = null;
    }
    
    // #76648: Autocomplete box is too close to text
    if(displayAboveCaret && Utilities.isMac()) {
        popupBounds.y -= 10;
    }
    
    popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
    popup.show();

    this.popupBounds = popupBounds;
    this.displayAboveCaret = displayAboveCaret;
}
 
Example 10
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
    * Returns a point where the given popup menu should be shown. The point is
    * calculated by adjusting the X and Y coordinates so that the popup menu
    * will not be clipped by the screen boundaries.
    * 
    * @param popup
    *            the popup menu
    * @param x
    *            the x position in screen coordinate
    * @param y
    *            the y position in screen coordinates
    * @return the point where the popup menu should be shown in screen
    *         coordinates
    */
   public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y
	+ sizeMenu.height);

Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
    Rectangle screenBounds = screensBounds[i];
    if (screenBounds.x <= x
	    && x <= (screenBounds.x + screenBounds.width)) {
	Dimension sizeScreen = screenBounds.getSize();
	sizeScreen.height -= 32; // Hack to help prevent menu being
				 // clipped by Windows/Linux/Solaris
				 // Taskbar.

	int xOffset = 0;
	if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
	    xOffset = -sizeMenu.width;

	int yOffset = 0;
	if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
	    yOffset = sizeScreen.height - bottomRightMenu.y;

	return new Point(x + xOffset, y + yOffset);
    }
}

return new Point(x, y); // ? that would mean that the top left point was
			// not on any screen.
   }
 
Example 11
Source File: XlibUtil.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 12
Source File: BasicGraphicsUtils.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 13
Source File: XlibUtil.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the given rectangle from one window to another.
 * Returns null if the translation is failed
 */
static Rectangle translateCoordinates(long src, long dst, Rectangle r)
{
    Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
    if (translatedLoc == null)
    {
        return null;
    }
    else
    {
        return new Rectangle(translatedLoc, r.getSize());
    }
}
 
Example 14
Source File: BasicGraphicsUtils.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 15
Source File: BasicGraphicsUtils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 16
Source File: BasicGraphicsUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the preferred size of the button.
 *
 * @param b an instance of {@code AbstractButton}
 * @param textIconGap a gap between text and icon
 * @return the preferred size of the button
 */
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
{
    if(b.getComponentCount() > 0) {
        return null;
    }

    Icon icon = b.getIcon();
    String text = b.getText();

    Font font = b.getFont();
    FontMetrics fm = b.getFontMetrics(font);

    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);

    SwingUtilities.layoutCompoundLabel(
        b, fm, text, icon,
        b.getVerticalAlignment(), b.getHorizontalAlignment(),
        b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
        viewR, iconR, textR, (text == null ? 0 : textIconGap)
    );

    /* The preferred size of the button is the size of
     * the text and icon rectangles plus the buttons insets.
     */

    Rectangle r = iconR.union(textR);

    Insets insets = b.getInsets();
    r.width += insets.left + insets.right;
    r.height += insets.top + insets.bottom;

    return r.getSize();
}
 
Example 17
Source File: BEComboBoxUI.java    From beautyeye with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the popup portion of the combo box.
 *
 * @return an instance of <code>ComboPopup</code>
 * @see ComboPopup
 */
protected ComboPopup createPopup() {
	return new BasicComboPopup( comboBox ){
		/** popupOffsetX是jb2011自定的属性,用于修正下拉框的弹出窗的X坐标 */
		private int popupOffsetX = UIManager.getInt("ComboBox.popupOffsetX");
		/** popupOffsetY是jb2011自定的属性,用于修正下拉框的弹出窗的Y坐标 */
		private int popupOffsetY = UIManager.getInt("ComboBox.popupOffsetY");
		
		//* copy from parent and modified by Jack Jiang
		/**
		 * Implementation of ComboPopup.show().
		 */
		public void show() {
			setListSelection(comboBox.getSelectedIndex());
			Point location = getPopupLocation();
			show( comboBox
					//以下x、y坐标修正代码由Jack Jiang增加
					, location.x + popupOffsetX //*~ popupOffsetX是自定属性,用于修改弹出窗的X坐标
					, location.y + popupOffsetY //*~ popupOffsetY是自定属性,用于修改弹出窗的Y坐标
			);
		}

		//* copy from parent and no modified
		/**
		 * Sets the list selection index to the selectedIndex. This 
		 * method is used to synchronize the list selection with the 
		 * combo box selection.
		 * 
		 * @param selectedIndex the index to set the list
		 */
		private void setListSelection(int selectedIndex) {
			if ( selectedIndex == -1 ) {
				list.clearSelection();
			}
			else {
				list.setSelectedIndex( selectedIndex );
				list.ensureIndexIsVisible( selectedIndex );
			}
		}

		//* copy from parent and no modified
		/**
		 * Calculates the upper left location of the Popup.
		 */
		private Point getPopupLocation() {
			Dimension popupSize = comboBox.getSize();
			Insets insets = getInsets();

			// reduce the width of the scrollpane by the insets so that the popup
			// is the same width as the combo box.
			popupSize.setSize(popupSize.width - (insets.right + insets.left), 
					getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
			Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
					popupSize.width, popupSize.height);
			Dimension scrollSize = popupBounds.getSize();
			Point popupLocation = popupBounds.getLocation();

			scroller.setMaximumSize( scrollSize );
			scroller.setPreferredSize( scrollSize );
			scroller.setMinimumSize( scrollSize );

			list.revalidate();

			return popupLocation;
		}
	};
}
 
Example 18
Source File: InfoBoxComponent.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Dimension render(Graphics2D graphics)
{
	if (image == null)
	{
		return new Dimension();
	}

	graphics.setFont(getSize() < DEFAULT_SIZE ? FontManager.getRunescapeSmallFont() : FontManager.getRunescapeFont());

	final int baseX = preferredLocation.x;
	final int baseY = preferredLocation.y;

	// Calculate dimensions
	final FontMetrics metrics = graphics.getFontMetrics();
	final int size = getSize();
	final Rectangle bounds = new Rectangle(baseX, baseY, size, size);

	// Render background
	final BackgroundComponent backgroundComponent = new BackgroundComponent();
	backgroundComponent.setBackgroundColor(backgroundColor);
	backgroundComponent.setRectangle(bounds);
	backgroundComponent.render(graphics);

	// Render image
	graphics.drawImage(
		image,
		baseX + (size - image.getWidth(null)) / 2,
		baseY + (size - image.getHeight(null)) / 2,
		null);

	// Render caption
	if (!Strings.isNullOrEmpty(text))
	{
		final TextComponent textComponent = new TextComponent();
		textComponent.setColor(color);
		textComponent.setText(text);
		textComponent.setPosition(new Point(baseX + ((size - metrics.stringWidth(text)) / 2), baseY + size - SEPARATOR));
		textComponent.render(graphics);
	}

	this.bounds.setBounds(bounds);
	return bounds.getSize();
}
 
Example 19
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 4 votes vote down vote up
private void setSizeAndDimensions(@NotNull JTable table,
    @NotNull JBPopup popup,
    @NotNull RelativePoint popupPosition,
    @NotNull List<UsageNode> data) {
  JComponent content = popup.getContent();
  Window window = SwingUtilities.windowForComponent(content);
  Dimension d = window.getSize();

  int width = calcMaxWidth(table);
  width = (int)Math.max(d.getWidth(), width);
  Dimension headerSize = ((AbstractPopup)popup).getHeaderPreferredSize();
  width = Math.max((int)headerSize.getWidth(), width);
  width = Math.max(myWidth, width);

  if (myWidth == -1) myWidth = width;
  int newWidth = Math.max(width, d.width + width - myWidth);

  myWidth = newWidth;

  int rowsToShow = Math.min(30, data.size());
  Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow);
  Rectangle rectangle = fitToScreen(dimension, popupPosition, table);
  dimension = rectangle.getSize();
  Point location = window.getLocation();
  if (!location.equals(rectangle.getLocation())) {
    window.setLocation(rectangle.getLocation());
  }

  if (!data.isEmpty()) {
    TableScrollingUtil.ensureSelectionExists(table);
  }
  table.setSize(dimension);
  //table.setPreferredSize(dimension);
  //table.setMaximumSize(dimension);
  //table.setPreferredScrollableViewportSize(dimension);


  Dimension footerSize = ((AbstractPopup)popup).getFooterPreferredSize();

  int newHeight = (int)(dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4/* invisible borders, margins etc*/;
  Dimension newDim = new Dimension(dimension.width, newHeight);
  window.setSize(newDim);
  window.setMinimumSize(newDim);
  window.setMaximumSize(newDim);

  window.validate();
  window.repaint();
  table.revalidate();
  table.repaint();
}
 
Example 20
Source File: LuckComboboxPopup.java    From littleluck with Apache License 2.0 3 votes vote down vote up
private Point getPopupLocation()
{
    Dimension popupSize = comboBox.getSize();

    Insets insets = getInsets();

    // reduce the width of the scrollpane by the insets so that the popup
    // is the same width as the combo box.
    popupSize.setSize(popupSize.width - (insets.right + insets.left),
            getPopupHeightForRowCount(comboBox.getMaximumRowCount()));

    Rectangle popupBounds = computePopupBounds(0,
            comboBox.getBounds().height, popupSize.width, popupSize.height);

    Dimension scrollSize = popupBounds.getSize();

    Point popupLocation = popupBounds.getLocation();

    scroller.setMaximumSize(scrollSize);

    scroller.setPreferredSize(scrollSize);

    scroller.setMinimumSize(scrollSize);

    list.revalidate();

    return popupLocation;
}