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

The following examples show how to use java.awt.Rectangle#setSize() . 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: PLabel.java    From PolyGlot with MIT License 6 votes vote down vote up
public void adaptLabelFont(JLabel l) {
    if (g == null || !resize) {
        return;
    }
    Rectangle r = l.getBounds();
    int fontSize = PGTUtil.PLABEL_MIN_FONT_SIZE;
    Font f = l.getFont();

    Rectangle r1 = new Rectangle();
    Rectangle r2 = new Rectangle();
    while (fontSize < PGTUtil.PLABEL_MAX_FONT_SIZE) {
        r1.setSize(getTextSize(l, f.deriveFont(f.getStyle(), fontSize)));
        r2.setSize(getTextSize(l, f.deriveFont(f.getStyle(), fontSize + 2)));
        if (r.contains(r1) && !r.contains(r2)) {
            break;
        }
        fontSize++;
    }

    setFont(f.deriveFont(f.getStyle(), fontSize));
    repaint();
}
 
Example 2
Source File: QuickSearchPopup.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void computePopupBounds (Rectangle result, JLayeredPane lPane, int modelSize) {
    Dimension cSize = comboBar.getSize();
    int width = getPopupWidth();
    Point location = new Point(cSize.width - width - 1, comboBar.getBottomLineY() - 1);
    if (SwingUtilities.getWindowAncestor(comboBar) != null) {
        location = SwingUtilities.convertPoint(comboBar, location, lPane);
    }
    result.setLocation(location);

    // hack to make jList.getpreferredSize work correctly
    // JList is listening on ResultsModel same as us and order of listeners
    // is undefined, so we have to force update of JList's layout data
    jList1.setFixedCellHeight(15);
    jList1.setFixedCellHeight(-1);
    // end of hack

    jList1.setVisibleRowCount(modelSize);
    Dimension preferredSize = jList1.getPreferredSize();

    preferredSize.width = width;
    preferredSize.height += statusPanel.getPreferredSize().height + 3;

    result.setSize(preferredSize);
}
 
Example 3
Source File: StockpileTab.java    From jeveassets with GNU General Public License v2.0 6 votes vote down vote up
public void scrollToSctockpile(final Stockpile stockpile) {
	SeparatorList.Separator<?> separator = getSeparator(stockpile);
	if (separator == null) {
		return;
	}
	if (separator.getLimit() > 0) { //Expanded: Scroll
		int row = EventListManager.indexOf(separatorList, separator.first()) - 1;
		Rectangle rect = jTable.getCellRect(row, 0, true);
		rect.setSize(jTable.getVisibleRect().getSize());
		jTable.scrollRectToVisible(rect);
	} else { //Collapsed: Expand and run again...
		try {
			separatorList.getReadWriteLock().writeLock().lock();
			separator.setLimit(Integer.MAX_VALUE);
		} finally {
			separatorList.getReadWriteLock().writeLock().unlock();
		}
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				scrollToSctockpile(stockpile);
			}
		});
	}
}
 
Example 4
Source File: AttackSprite.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Rectangle getBounds() {
    makePoly();
    // set bounds
    bounds = new Rectangle(attackPoly.getBounds());
    bounds.setSize(bounds.getSize().width + 1,
            bounds.getSize().height + 1);
    // move poly to upper right of image
    attackPoly.translate(-bounds.getLocation().x,
            -bounds.getLocation().y);

    return bounds;
}
 
Example 5
Source File: Utility.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adjust the crop rectangle to fit within the image it is cropping. Also
 * ensure the area is square.
 *
 * @param image    The image being cropped
 * @param cropRect The rectangle defining the cropping area. This may be updated.
 */
public static void adjustRectToFitImage(RenderedImage image, Rectangle cropRect)
{
	// Make sure the rectangle is not too big
	if (cropRect.width > image.getWidth())
	{
		cropRect.width = image.getWidth();
	}
	if (cropRect.height > image.getHeight())
	{
		cropRect.height = image.getHeight();
	}

	// Make it square
	int dimension = Math.min(cropRect.width, cropRect.height);
	cropRect.setSize(dimension, dimension);

	// Now adjust the origin point so the box is within the image
	if ((cropRect.x + cropRect.width) > image.getWidth())
	{
		cropRect.x = image.getWidth() - cropRect.width;
	}
	if ((cropRect.y + cropRect.height) > image.getHeight())
	{
		cropRect.y = image.getHeight() - cropRect.height;
	}
}
 
Example 6
Source File: SavedRectangle.java    From haxademic with MIT License 5 votes vote down vote up
public void mouseEvent(MouseEvent event) {
	switch (event.getAction()) {
		case MouseEvent.PRESS:
			mouseStartPoint.setLocation( event.getX() + rectOffset.x, event.getY() + rectOffset.y );
			if(rectangle.contains(mouseStartPoint) && SavedRectangle.curDragging == null) {
				isDragging = true;
				SavedRectangle.curDragging = this;
				float cornerClickDist = (float) mouseStartPoint.distance(rectangle.x + rectangle.width, rectangle.y + rectangle.height);
				if(cornerClickDist < 15) {
					isResizing = true;
				} 
				rectangleMove = new Rectangle(rectangle); // build temp rectangle for moving
			}
			break;
		case MouseEvent.RELEASE:
			if(isDragging == true) {
				updateRectangle(rectangleMove);
				SavedRectangle.curDragging = null;
				rectangleMove = null;
				isDragging = false;
				isResizing = false;
			}
			break;
		case MouseEvent.MOVE:
			break;
		case MouseEvent.DRAG:
			if(isDragging == true) {
				mouseMovePoint.setLocation( event.getX() + rectOffset.x, event.getY() + rectOffset.y );
				int mouseDeltaX = mouseMovePoint.x - mouseStartPoint.x;
				int mouseDeltaY = mouseMovePoint.y - mouseStartPoint.y;
				if(isResizing == false) {
					rectangleMove.setLocation(rectangle.x + mouseDeltaX, rectangle.y + mouseDeltaY );
				} else {
					rectangleMove.setSize(rectangle.width + mouseDeltaX, rectangle.height + mouseDeltaY );
				}
			}
			break;
	}
}
 
Example 7
Source File: MMDGraphEditor.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void onEnsureVisibilityOfTopic(@Nonnull final MindMapPanel source, @Nonnull final Topic topic) {
  mindMapPanel.doLayout();

  final AbstractElement element = (AbstractElement) topic.getPayload();
  if (element == null) {
    return;
  }

  final Rectangle2D orig = element.getBounds();
  final int GAP = 30;

  final Rectangle bounds = orig.getBounds();
  bounds.setLocation(Math.max(0, bounds.x - GAP), Math.max(0, bounds.y - GAP));
  bounds.setSize(bounds.width + GAP * 2, bounds.height + GAP * 2);

  final JViewport viewport = mainScrollPane.getViewport();
  final Rectangle visible = viewport.getViewRect();

  if (visible.contains(bounds)) {
    return;
  }

  bounds.setLocation(bounds.x - visible.x, bounds.y - visible.y);

  mainScrollPane.revalidate();
  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      viewport.scrollRectToVisible(bounds);
      mainScrollPane.repaint();
    }
  });
}
 
Example 8
Source File: Utility.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adjust the crop rectangle to fit within the image it is cropping. Also
 * ensure the area is square.
 *
 * @param image    The image being cropped
 * @param cropRect The rectangle defining the cropping area. This may be updated.
 */
public static void adjustRectToFitImage(RenderedImage image, Rectangle cropRect)
{
	// Make sure the rectangle is not too big
	if (cropRect.width > image.getWidth())
	{
		cropRect.width = image.getWidth();
	}
	if (cropRect.height > image.getHeight())
	{
		cropRect.height = image.getHeight();
	}

	// Make it square
	int dimension = Math.min(cropRect.width, cropRect.height);
	cropRect.setSize(dimension, dimension);

	// Now adjust the origin point so the box is within the image
	if ((cropRect.x + cropRect.width) > image.getWidth())
	{
		cropRect.x = image.getWidth() - cropRect.width;
	}
	if ((cropRect.y + cropRect.height) > image.getHeight())
	{
		cropRect.y = image.getHeight() - cropRect.height;
	}
}
 
Example 9
Source File: FlyOverSprite.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Rectangle getBounds() {
    if (true) {
        makePoly();
    }
    // set bounds
    bounds = new Rectangle(flyOverPoly.getBounds());
    bounds.setSize(bounds.getSize().width + 1, bounds.getSize().height + 1);
    return bounds;
}
 
Example 10
Source File: MovementSprite.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Rectangle getBounds() {
    makePoly();
    // set bounds
    bounds = new Rectangle(movePoly.getBounds());
    bounds.setSize(bounds.getSize().width + 1,
            bounds.getSize().height + 1);
    // move poly to upper right of image
    movePoly.translate(-bounds.getLocation().x, -bounds.getLocation().y);

    return bounds;
}
 
Example 11
Source File: C3Sprite.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Rectangle getBounds() {
    makePoly();
    // set bounds
    bounds = new Rectangle(c3Poly.getBounds());
    bounds.setSize(bounds.getSize().width + 1,
            bounds.getSize().height + 1);

    // move poly to upper right of image
    c3Poly.translate(-bounds.getLocation().x, -bounds.getLocation().y);
    image = null;

    return bounds;
}
 
Example 12
Source File: AttackSprite.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If we have build full arrow already with single attack and have got
 * counter attack from our target lately - lets change arrow to halved.
 */
public void rebuildToHalvedPolygon() {
    attackPoly = new StraightArrowPolygon(a, t, (int) (8 * this.boardView1.scale),
            (int) (12 * this.boardView1.scale), true);
    // set bounds
    bounds = new Rectangle(attackPoly.getBounds());
    bounds.setSize(bounds.getSize().width + 1,
            bounds.getSize().height + 1);
    // move poly to upper right of image
    attackPoly.translate(-bounds.getLocation().x,
            -bounds.getLocation().y);
}
 
Example 13
Source File: EpsGraphics2D.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the bounding rectangle of the current clipping area.
 */
public Rectangle getClipBounds(Rectangle r) {
  if(_clip==null) {
    return r;
  }
  Rectangle rect = getClipBounds();
  r.setLocation((int) rect.getX(), (int) rect.getY());
  r.setSize((int) rect.getWidth(), (int) rect.getHeight());
  return r;
}
 
Example 14
Source File: ShowUsagesAction.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private static Rectangle fitToScreen(@NotNull Dimension newDim,
    @NotNull RelativePoint popupPosition, JTable table) {
  Rectangle rectangle = new Rectangle(popupPosition.getScreenPoint(), newDim);
  ScreenUtil.fitToScreen(rectangle);
  if (rectangle.getHeight() != newDim.getHeight()) {
    int newHeight = (int) rectangle.getHeight();
    int roundedHeight = newHeight - newHeight % table.getRowHeight();
    rectangle.setSize((int) rectangle.getWidth(), Math.max(roundedHeight, table.getRowHeight()));
  }
  return rectangle;
}
 
Example 15
Source File: Screen.java    From screenstudio with GNU General Public License v3.0 5 votes vote down vote up
public static Rectangle captureWindowArea() throws IOException {
    Rectangle r = new Rectangle();
    System.out.println("Capture Window Area");
    Process p = Runtime.getRuntime().exec("xwininfo");
    InputStream in = p.getInputStream();
    InputStreamReader isr = new InputStreamReader(in);
    BufferedReader reader = new BufferedReader(isr);
    String line = reader.readLine();
    int x = 0, y = 0, w = 0, h = 0;
    while (line != null) {
        System.out.println(line);
        if (line.trim().startsWith("Absolute upper-left X:")) {
            x = new Integer(line.trim().replaceAll(" ", "").split(":")[1]);
        } else if (line.trim().startsWith("Absolute upper-left Y:")) {
            y = new Integer(line.trim().replaceAll(" ", "").split(":")[1]);
        } else if (line.trim().startsWith("Width:")) {
            w = new Integer(line.trim().replaceAll(" ", "").split(":")[1]);
        } else if (line.trim().startsWith("Height:")) {
            h = new Integer(line.trim().replaceAll(" ", "").split(":")[1]);
        }
        line = reader.readLine();
    }
    r.setSize(w, h);
    r.setLocation(x, y);
    in.close();
    isr.close();
    reader.close();
    p.destroy();
    return r;
}
 
Example 16
Source File: GraphBiLayout.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private Rectangle findMaxBound(List<GraphLocation> nodes) {
    Rectangle maxBounds = new Rectangle();
    for (GraphLocation n : nodes) {
        Rectangle bounds = n.getBounds();
        if (maxBounds.width < bounds.width) {
            maxBounds.setSize(bounds.width, maxBounds.height);
        }
        if (maxBounds.height < bounds.height) {
            maxBounds.setSize(maxBounds.width, bounds.height);
        }
    }
    return maxBounds;
}
 
Example 17
Source File: mxCellEditor.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the bounds to be used for the editor.
 */
public Rectangle getEditorBounds(mxCellState state, double scale)
{
	mxIGraphModel model = state.getView().getGraph().getModel();
	Rectangle bounds = null;

	if (useLabelBounds(state))
	{
		bounds = state.getLabelBounds().getRectangle();
		bounds.height += 10;
	}
	else
	{
		bounds = state.getRectangle();
	}

	// Applies the horizontal and vertical label positions
	if (model.isVertex(state.getCell()))
	{
		String horizontal = mxUtils.getString(state.getStyle(),
				mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_CENTER);

		if (horizontal.equals(mxConstants.ALIGN_LEFT))
		{
			bounds.x -= state.getWidth();
		}
		else if (horizontal.equals(mxConstants.ALIGN_RIGHT))
		{
			bounds.x += state.getWidth();
		}

		String vertical = mxUtils.getString(state.getStyle(),
				mxConstants.STYLE_VERTICAL_LABEL_POSITION,
				mxConstants.ALIGN_MIDDLE);

		if (vertical.equals(mxConstants.ALIGN_TOP))
		{
			bounds.y -= state.getHeight();
		}
		else if (vertical.equals(mxConstants.ALIGN_BOTTOM))
		{
			bounds.y += state.getHeight();
		}
	}

	bounds.setSize(
			(int) Math.max(bounds.getWidth(),
					Math.round(minimumWidth * scale)),
			(int) Math.max(bounds.getHeight(),
					Math.round(minimumHeight * scale)));

	return bounds;
}
 
Example 18
Source File: BackgroundComponent.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Dimension render(Graphics2D graphics)
{
	Color outsideStrokeColor = new Color(
		(int) (backgroundColor.getRed() * OUTER_COLOR_OFFSET),
		(int) (backgroundColor.getGreen() * OUTER_COLOR_OFFSET),
		(int) (backgroundColor.getBlue() * OUTER_COLOR_OFFSET),
		Math.min(255, (int) (backgroundColor.getAlpha() * ALPHA_COLOR_OFFSET))
	);

	Color insideStrokeColor = new Color(
		Math.min(255, (int) (backgroundColor.getRed() * INNER_COLOR_OFFSET)),
		Math.min(255, (int) (backgroundColor.getGreen() * INNER_COLOR_OFFSET)),
		Math.min(255, (int) (backgroundColor.getBlue() * INNER_COLOR_OFFSET)),
		Math.min(255, (int) (backgroundColor.getAlpha() * ALPHA_COLOR_OFFSET))
	);

	// Render background
	if (fill)
	{
		graphics.setColor(backgroundColor);
		graphics.fill(rectangle);
	}

	// Render outside stroke
	final Rectangle outsideStroke = new Rectangle();
	outsideStroke.setLocation(rectangle.x, rectangle.y);
	outsideStroke.setSize(rectangle.width - BORDER_OFFSET / 2, rectangle.height - BORDER_OFFSET / 2);
	graphics.setColor(outsideStrokeColor);
	graphics.draw(outsideStroke);

	// Render inside stroke
	final Rectangle insideStroke = new Rectangle();
	insideStroke.setLocation(rectangle.x + BORDER_OFFSET / 2, rectangle.y + BORDER_OFFSET / 2);
	insideStroke.setSize(rectangle.width - BORDER_OFFSET - BORDER_OFFSET / 2,
			rectangle.height - BORDER_OFFSET - BORDER_OFFSET / 2);
	graphics.setColor(insideStrokeColor);
	graphics.draw(insideStroke);

	return new Dimension(rectangle.getSize());
}
 
Example 19
Source File: FlatMenuItemRenderer.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect,
	Rectangle accelRect, Rectangle arrowRect, Rectangle labelRect )
{
	boolean isTopLevelMenu = isTopLevelMenu( menuItem );

	// layout arrow
	if( !isTopLevelMenu && arrowIcon != null ) {
		arrowRect.width = arrowIcon.getIconWidth();
		arrowRect.height = arrowIcon.getIconHeight();
	} else
		arrowRect.setSize( 0, 0 );
	arrowRect.y = viewRect.y + centerOffset( viewRect.height, arrowRect.height );

	// layout accelerator
	String accelText = getAcceleratorText();
	if( accelText != null ) {
		FontMetrics accelFm = menuItem.getFontMetrics( acceleratorFont );
		accelRect.width = SwingUtilities.computeStringWidth( accelFm, accelText );
		accelRect.height = accelFm.getHeight();

		accelRect.y = viewRect.y + centerOffset( viewRect.height, accelRect.height );
	} else
		accelRect.setBounds( 0, 0, 0, 0 );

	// compute horizontal positions of accelerator and arrow
	int accelArrowGap = !isTopLevelMenu ? scale( acceleratorArrowGap ) : 0;
	if( menuItem.getComponentOrientation().isLeftToRight() ) {
		// left-to-right
		arrowRect.x = viewRect.x + viewRect.width - arrowRect.width;
		accelRect.x = arrowRect.x - accelArrowGap - accelRect.width;
	} else {
		// right-to-left
		arrowRect.x = viewRect.x;
		accelRect.x = arrowRect.x + accelArrowGap + arrowRect.width;
	}

	// width of accelerator, arrow and gap
	int accelArrowWidth = accelRect.width + arrowRect.width;
	if( accelText != null )
		accelArrowWidth += scale( !isTopLevelMenu ? textAcceleratorGap : menuItem.getIconTextGap() );
	if( !isTopLevelMenu && arrowIcon != null ) {
		if( accelText == null )
			accelArrowWidth += scale( textNoAcceleratorGap );
		accelArrowWidth += scale( acceleratorArrowGap );
	}

	// label rectangle is view rectangle subtracted by accelerator, arrow and gap
	labelRect.setBounds( viewRect );
	labelRect.width -= accelArrowWidth;
	if( !menuItem.getComponentOrientation().isLeftToRight() )
		labelRect.x += accelArrowWidth;

	// layout icon and text
	SwingUtilities.layoutCompoundLabel( menuItem,
		menuItem.getFontMetrics( menuItem.getFont() ), menuItem.getText(), getIconForLayout(),
		menuItem.getVerticalAlignment(), menuItem.getHorizontalAlignment(),
		menuItem.getVerticalTextPosition(), menuItem.getHorizontalTextPosition(),
		labelRect, iconRect, textRect, scale( menuItem.getIconTextGap() ) );
}
 
Example 20
Source File: AnimatedLayout.java    From stendhal with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Calculate interpolated bounds based on the initial and final bounds,
 * and the state of progress.
 *
 * @param startBounds initial bounds
 * @param finalBounds final bounds
 * @param progress state of progress
 * @return interpolated bounds
 */
private Rectangle interpolate(Rectangle startBounds, Rectangle finalBounds, double progress) {
	Rectangle bounds = new Rectangle();
	bounds.setLocation(interpolate(startBounds.getLocation(), finalBounds.getLocation(), progress));
	bounds.setSize(interpolate(startBounds.getSize(), finalBounds.getSize(), progress));

	return bounds;
}