下面列出了javax.swing.SwingUtilities#convertRectangle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void recalculateBounds() {
if (adjustingBounds > 0)
return;
adjustingBounds++;
try {
if (span.isVisible() == false || span.getParent() == null
|| getParent() == null) {
setVisible(false);
return;
}
setVisible(true);
Rectangle bounds = span.getBounds();
bounds = SwingUtilities.convertRectangle(span.getParent(), bounds,
this.getParent());
setBounds(bounds);
recalculateHighlight(true);
} finally {
adjustingBounds--;
}
}
@Override
public int dropIndexOfPoint( Point location ) {
int res = -1;
location = SwingUtilities.convertPoint( this, location, table );
TabData tab = table.getTabAt( location );
if( null != tab ) {
res = getModel().indexOf( tab );
Rectangle rect = getTabBounds( res );
rect = SwingUtilities.convertRectangle( this, rect, table );
if( orientation == JTabbedPane.VERTICAL ) {
if( location.y <= rect.y + rect.height/2 ) {
res = Math.max( 0, res );
} else {
res++;
}
} else {
if( location.x <= rect.x + rect.width/2 ) {
res = Math.max( 0, res );
} else {
res++;
}
}
}
return res;
}
protected void repositionDialog() {
Point topLeft = new Point(0, 0);
topLeft = SwingUtilities.convertPoint(dialogAnchor, topLeft, window1);
int x = window1.getX() - window2.getWidth() / 2
+ dialogAnchor.getWidth() / 2 + topLeft.x;
int y = topLeft.y + dialogAnchor.getHeight() + 1 + window1.getY();
Rectangle optionsBounds = new Rectangle(x, y, window2.getWidth(),
window2.getHeight());
SwingUtilities.convertRectangle(dialogAnchor, optionsBounds, window1);
window2.setBounds(optionsBounds);
}
@Override
public void paint(Graphics g) {
g = g.create();
Rectangle clipping = (Rectangle) getClientProperty(CLIPPING);
if (clipping != null) {
clipping = SwingUtilities.convertRectangle(
RowLayout.this.panel, clipping, this);
g.clipRect(clipping.x, clipping.y, clipping.width,
clipping.height);
}
super.paint(g);
g.dispose();
}
private void cropListingWithStatusArea() {
CodeBrowserPlugin cb = getPlugin(tool, CodeBrowserPlugin.class);
ListingPanel listingPanel = cb.getListingPanel();
Window window = windowForComponent(listingPanel);
StatusBar statusBar = getStatusBar();
int statusHeight = statusBar.getHeight();
DockableComponent dc = getDockableComponent(listingPanel);
Rectangle cropBounds = dc.getBounds();
cropBounds = SwingUtilities.convertRectangle(dc.getParent(), cropBounds, window);
cropBounds.height += statusHeight;
crop(cropBounds);
}
/** Paint each of the given nodes that is inside of the clips */
private void paintNodes(List<GTreeNode> nodes, Graphics g) {
TreeCellRenderer cellRenderer = tree.getCellRenderer();
Rectangle clip = g.getClipBounds();
Container parent = tree.getParent();
int yOffset = 0;
try {
for (GTreeNode node : nodes) {
int row = tree.getRowForPath(node.getTreePath());
Rectangle rowBounds = tree.getRowBounds(row);
rowBounds = SwingUtilities.convertRectangle(tree, rowBounds, parent);
if (clip.y > rowBounds.y + rowBounds.height) {
continue; // above our clip
}
if (clip.y + clip.height < rowBounds.y + rowBounds.height) {
// painted past the bounds of our clip
return;
}
Component renderer =
cellRenderer.getTreeCellRendererComponent(tree, node, true, true,
node.isLeaf(), row, false);
renderer.setSize(renderer.getPreferredSize());
// move down the point in our graphics space into which we will paint
yOffset += rowBounds.height;
g.translate(0, rowBounds.height);
renderer.paint(g);
}
}
finally {
// restore the point into graphics that we will paint
g.translate(0, -yOffset);
}
}
protected JViewport createViewport() {
return new JViewport() {
public void scrollRectToVisible(Rectangle aRect) {
if (getView() instanceof JTextComponent) {
try {
JTextComponent tc = (JTextComponent)getView();
Caret caret = tc.getCaret();
Rectangle selStart = tc.modelToView(Math.min(caret.getDot(), caret.getMark()));
Rectangle selEnd = tc.modelToView(Math.max(caret.getDot(), caret.getMark()));
int x = Math.min(selStart.x, selEnd.x);
int xx = Math.max(selStart.x + selStart.width, selEnd.x + selEnd.width);
int y = Math.min(selStart.y, selEnd.y);
int yy = Math.max(selStart.y + selStart.height, selEnd.y + selEnd.height);
Rectangle r = new Rectangle(x, y, xx - x, yy - y);
super.scrollRectToVisible(SwingUtilities.convertRectangle(tc, r, this));
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
} else {
super.scrollRectToVisible(aRect);
}
aRect = SwingUtilities.convertRectangle(this, aRect, getParent());
((JComponent)getParent()).scrollRectToVisible(aRect);
}
};
}
private Rectangle getImageBounds() {
if (!changed) {
return rect;
}
Component c = tabDisplayer;
r2.setBounds (0, 0, c.getWidth(), c.getHeight());
Rectangle dispBounds = SwingUtilities.convertRectangle(c, r2,
this);
if (orientation == TabDisplayer.ORIENTATION_WEST) {
rect.x = dispBounds.x + dispBounds.width;
rect.y = dispBounds.y;
rect.width = Math.round (inc * d.width);
rect.height = dispBounds.height;
} else if (orientation == TabDisplayer.ORIENTATION_EAST) {
rect.width = Math.round (inc * d.width);
rect.height = dispBounds.height;
rect.x = dispBounds.x - rect.width;
rect.y = dispBounds.y;
} else if (orientation == TabDisplayer.ORIENTATION_SOUTH) {
rect.width = dispBounds.width;
rect.height = Math.round(inc * d.height);
rect.x = dispBounds.x;
rect.y = dispBounds.y - rect.height;
} else if (orientation == TabDisplayer.ORIENTATION_NORTH) {
rect.x = dispBounds.x;
rect.y = dispBounds.y + dispBounds.height;
rect.width = dispBounds.width;
rect.height = Math.round(inc * d.height);
}
changed = false;
return rect;
}
private Rectangle getImageBounds() {
if (!changed) {
return rect;
}
Component c = tabDisplayer;
r2.setBounds (0, 0, c.getWidth(), c.getHeight());
Rectangle dispBounds = SwingUtilities.convertRectangle(c, r2,
this);
if (orientation == TabDisplayer.ORIENTATION_WEST) {
rect.x = dispBounds.x + dispBounds.width;
rect.y = dispBounds.y;
rect.width = Math.round (inc * d.width);
rect.height = dispBounds.height;
} else if (orientation == TabDisplayer.ORIENTATION_EAST) {
rect.width = Math.round (inc * d.width);
rect.height = dispBounds.height;
rect.x = dispBounds.x - rect.width;
rect.y = dispBounds.y;
} else if (orientation == TabDisplayer.ORIENTATION_SOUTH) {
rect.width = dispBounds.width;
rect.height = Math.round(inc * d.height);
rect.x = dispBounds.x;
rect.y = dispBounds.y - rect.height;
} else if (orientation == TabDisplayer.ORIENTATION_NORTH) {
rect.x = dispBounds.x;
rect.y = dispBounds.y + dispBounds.height;
rect.width = dispBounds.width;
rect.height = Math.round(inc * d.height);
}
changed = false;
return rect;
}
private BufferedImage createTabImage() {
GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
//the tab rectangle must be painted by top-level window otherwise the transparent
//button icons will be messed up
Window parentWindow = SwingUtilities.getWindowAncestor(container.getComponent());
Rectangle rect = SwingUtilities.convertRectangle(container.getComponent(), tabRectangle, parentWindow);
BufferedImage res = config.createCompatibleImage(tabRectangle.width, tabRectangle.height);
Graphics2D g = res.createGraphics();
g.translate(-rect.x, -rect.y);
g.setClip(rect);
parentWindow.paint(g);
return res;
}
@Override
protected Shape getDropIndication( TopComponent draggedTC, Point location ) {
location = SwingUtilities.convertPoint( getComponent(), location, getTabDisplayer() );
Path2D res = new Path2D.Double();
Rectangle tabRect = getTabDisplayer().dropIndication( draggedTC, location );
if( null != tabRect ) {
tabRect = SwingUtilities.convertRectangle( getTabDisplayer(), tabRect, container );
res.append( tabRect, false );
}
res.append( container.getContentArea(), false );
return res;
}
@Override
public Rectangle getTabBounds( int tabIndex ) {
Rectangle res = getTabDisplayer().getTabBounds( tabIndex );
if( null != res )
res = SwingUtilities.convertRectangle( getTabDisplayer(), res, container );
return res;
}
Rectangle getTabBounds( int tabIndex ) {
TabData tab = tabModel.getTab( tabIndex );
if( null == tab )
return null;
for( SingleRowTabTable table : rows ) {
if( table.hasTabIndex( tabIndex ) ) {
Rectangle rect = table.getTabBounds( tabIndex );
if( null != rect ) {
rect = SwingUtilities.convertRectangle( table, rect, container );
}
return rect;
}
}
return null;
}
@Override
public Rectangle getTabBounds( int tabIndex ) {
Rectangle res = table.getTabBounds( tabIndex );
if( null != res )
res = SwingUtilities.convertRectangle( table, res, this );
return res;
}
public void paint(Graphics g) {
super.paint(g);
if (hover != null) {
Rectangle b = SwingUtilities.convertRectangle(
hover.getParent(), hover.getBounds(), this);
g.setColor(Color.RED);
g.drawRect(b.x, b.y, b.width, b.height);
}
}
public void paint(Graphics g) {
super.paint(g);
if (hover != null) {
Rectangle b = SwingUtilities.convertRectangle(
hover.getParent(), hover.getBounds(), this);
g.setColor(Color.RED);
g.drawRect(b.x, b.y, b.width, b.height);
}
}
private Rectangle getInstructionLabelBounds() {
JLabel label = (JLabel) getInstanceField("instructionLabel", plugin);
Window window = windowForComponent(label);
Rectangle bounds = label.getBounds();
return SwingUtilities.convertRectangle(label.getParent(), bounds, window);
}
/** Checks the position of the tabbed container relative to its parent
* window, and potentially updates its orientation client property.
*
* @see TabDisplayer#PROP_ORIENTATION
*/
protected final void updateOrientation() {
if (!container.isDisplayable()) {
return;
}
if (Boolean.FALSE.equals(container.getClientProperty (TabbedContainer.PROP_MANAGE_TAB_POSITION))) {
//The client has specified that it does not want automatic management
//of the displayer orientation
return;
}
Object currOrientation = tabDisplayer.getClientProperty(TabDisplayer.PROP_ORIENTATION);
Container window = container.getTopLevelAncestor();
Rectangle containerBounds = container.getBounds();
containerBounds = SwingUtilities.convertRectangle(container, containerBounds, window);
boolean longestIsVertical = containerBounds.width < containerBounds.height;
int distanceToLeft = containerBounds.x;
int distanceToTop = containerBounds.y;
int distanceToRight = window.getWidth() - (containerBounds.x + containerBounds.width);
int distanceToBottom = window.getHeight() - (containerBounds.y + containerBounds.height);
Object orientation;
if (!longestIsVertical) {
if (distanceToBottom > distanceToTop) {
orientation = TabDisplayer.ORIENTATION_NORTH;
} else {
orientation = TabDisplayer.ORIENTATION_SOUTH;
}
} else {
if (distanceToLeft > distanceToRight) {
orientation = TabDisplayer.ORIENTATION_EAST;
} else {
orientation = TabDisplayer.ORIENTATION_WEST;
}
}
if (currOrientation != orientation) {
tabDisplayer.putClientProperty(
TabDisplayer.PROP_ORIENTATION, orientation);
container.validate();
}
}
private static void configureBalloon( Balloon balloon, JLayeredPane pane, JComponent ownerComp ) {
Rectangle ownerCompBounds = ownerComp.getBounds();
ownerCompBounds = SwingUtilities.convertRectangle( ownerComp.getParent(), ownerCompBounds, pane );
int paneWidth = pane.getWidth();
int paneHeight = pane.getHeight();
Dimension balloonSize = balloon.getPreferredSize();
balloonSize.height += Balloon.ARC;
//first try lower right corner
if( ownerCompBounds.x + ownerCompBounds.width + balloonSize.width < paneWidth
&&
ownerCompBounds.y + ownerCompBounds.height + balloonSize.height + Balloon.ARC < paneHeight ) {
balloon.setArrowLocation( GridBagConstraints.SOUTHEAST );
balloon.setBounds( ownerCompBounds.x+ownerCompBounds.width-Balloon.ARC/2,
ownerCompBounds.y+ownerCompBounds.height, balloonSize.width+Balloon.ARC, balloonSize.height );
//upper right corner
} else if( ownerCompBounds.x + ownerCompBounds.width + balloonSize.width < paneWidth
&&
ownerCompBounds.y - balloonSize.height - Balloon.ARC > 0 ) {
balloon.setArrowLocation( GridBagConstraints.NORTHEAST );
balloon.setBounds( ownerCompBounds.x+ownerCompBounds.width-Balloon.ARC/2,
ownerCompBounds.y-balloonSize.height, balloonSize.width+Balloon.ARC, balloonSize.height );
//lower left corner
} else if( ownerCompBounds.x - balloonSize.width > 0
&&
ownerCompBounds.y + ownerCompBounds.height + balloonSize.height + Balloon.ARC < paneHeight ) {
balloon.setArrowLocation( GridBagConstraints.SOUTHWEST );
balloon.setBounds( ownerCompBounds.x-balloonSize.width+Balloon.ARC/2,
ownerCompBounds.y+ownerCompBounds.height, balloonSize.width+Balloon.ARC, balloonSize.height );
//upper left corent
} else {
balloon.setArrowLocation( GridBagConstraints.NORTHWEST );
balloon.setBounds( ownerCompBounds.x-balloonSize.width/*+Balloon.ARC/2*/,
ownerCompBounds.y-balloonSize.height, balloonSize.width+Balloon.ARC, balloonSize.height );
}
}
/**
* Converts a {@code rectangle} in {@code componentPane} coordinates to
* a corresponding rectangle in {@code innerPane} coordinates.
*
* @param rectangle rectangle in {@code componentPane} coordinates.
* @return rectangle in {@code innerPane} coordinates.
*/
Rectangle fromComponentPane(Rectangle rectangle) {
return SwingUtilities.convertRectangle(componentPane, rectangle, innerPane);
}