下面列出了java.awt.Rectangle#setSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
});
}
}
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();
}
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;
}
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;
}
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;
}
/**
* 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;
}
@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;
}
/**
* 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);
}
@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;
}
@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;
}
@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;
}
/**
* 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;
}
}
@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();
}
});
}
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;
}
}
/**
* 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;
}
}
/**
* 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;
}
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() ) );
}
@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());
}
/**
* 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;
}