下面列出了javax.swing.SwingUtilities#convertPointFromScreen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void moveIt(Point location) {
Point oldLocation = this.location;
SwingUtilities.convertPointFromScreen(location, this);
this.location = location;
Rectangle newClip = new Rectangle(location.x - image.getWidth() / 2, location.y - image.getHeight() / 2,
image.getWidth(), image.getHeight());
newClip.add(new Rectangle(oldLocation.x - image.getWidth() / 2, oldLocation.y - image.getHeight() / 2,
image.getWidth(), image.getHeight()));
newClip.add(new Rectangle(oldLocation.x - image.getWidth() / 2, oldLocation.y - image.getHeight() / 2,
shadow.getWidth(), shadow.getHeight()));
newClip.add(new Rectangle(location.x - image.getWidth() / 2, location.y - image.getHeight() / 2,
shadow.getWidth(), shadow.getHeight()));
repaint(newClip);
}
protected void endDrag(DragSourceDropEvent e) {
if (draggingComponent != null) {
Point p = e.getLocation();
SwingUtilities.convertPointFromScreen(p, this);
if (contains(p) == false) {
// adding this extra ability to commit changes
// makes sure if you simply drag an element
// off the toolbar: it gets recorded.
setContents(getContents(new Point(-1000, -1000)));
}
}
draggingComponent = null;
/**
* TODO: is there some way when a drop ends if it IS accepted to NOT
* show the image slide back to its original location? For example: 1.
* Open this demo app 2. Click "Customize" 3. Drag a component off the
* toolbar in the window into nothingness. Note the *image* slides back
* to where it came from, because this is how the OS shows the drag was
* not accepted. But even though it was not accepted by any other
* entity: the drag was successful, and showing that sliding image is
* incorrect.
*/
}
/**
*
* @param event
*/
private void showTooltipWindow (MouseEvent event) {
Point p = new Point(event.getPoint());
SwingUtilities.convertPointToScreen(p, this);
Point p2 = new Point(p);
SwingUtilities.convertPointFromScreen(p2, textComponent);
// annotation for target line
AnnotateLine al = null;
if (!elementAnnotations.isEmpty()) {
al = getAnnotateLine(getLineFromMouseEvent(event));
}
/**
* al.getCommitMessage() != null - since commit messages are initialized separately from the AL constructor
*/
if (al != null && al.getCommitMessage() != null) {
TooltipWindow ttw = new TooltipWindow(this, al);
ttw.show(new Point(p.x - p2.x, p.y));
}
}
/**
* Get the corresponding local point wrt a containing component
*
* @param component the provided component
* @return the local point, wrt component top left corner
*/
public Point getLocalPoint (Component component)
{
Point localPoint = new Point(x, y);
SwingUtilities.convertPointFromScreen(localPoint, component);
return localPoint;
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
public void showIt(BufferedImage image, Point location) {
this.image = image;
this.shadow = new ShadowRenderer(5, 0.3f, Color.BLACK).createShadow(image);
SwingUtilities.convertPointFromScreen(location, this);
this.location = location;
setVisible(true);
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private Point getMenuLocation()
{
Point menuPoint = MouseInfo.getPointerInfo().getLocation();
JScrollPane scrollPane = (JScrollPane)effectsPanel.getList().getParent().getParent();
SwingUtilities.convertPointFromScreen(menuPoint, scrollPane);
int listLocation = effectsPanel.getY();
return new Point(menuPoint.x, menuPoint.y + listLocation);
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void update( Point locationOnScreen ) {
if( null != locationOnScreen ) {
SwingUtilities.convertPointFromScreen( locationOnScreen, content );
lastLocation = locationOnScreen;
horizontalSplit = calculateOrientation();
lastLocation.x = Math.max( 0, lastLocation.x );
lastLocation.y = Math.max( 0, lastLocation.y );
lastLocation.x = Math.min( content.getWidth()-splitterWidth, lastLocation.x );
lastLocation.y = Math.min( content.getHeight()-splitterWidth, lastLocation.y );
content.repaint();
} else {
lastLocation = null;
}
}
private void repaintBadge(JComponent field) {
Point p = field.getLocationOnScreen();
SwingUtilities.convertPointFromScreen(p, this);
int x = p.x - warningIcon.getWidth() / 2;
int y = (int) (p.y + field.getHeight() - warningIcon.getHeight() / 1.5);
repaint(x, y, warningIcon.getWidth(), warningIcon.getHeight());
}
/** Shows given popup on given coordinations and takes care about the
* situation when menu can exceed screen limits.
* Copied from org.netbeans.core.windows.frames.DefaultContainerImpl
*/
private static void showPopupMenu(JPopupMenu popup, Point p, Component comp) {
SwingUtilities.convertPointToScreen(p, comp);
Dimension popupSize = popup.getPreferredSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (p.x + popupSize.width > screenSize.width) {
p.x = screenSize.width - popupSize.width;
}
if (p.y + popupSize.height > screenSize.height) {
p.y = screenSize.height - popupSize.height;
}
SwingUtilities.convertPointFromScreen(p, comp);
popup.show(comp, p.x, p.y);
}
public static Component findComponentAt(final Container c, final Point sp) {
final Point cp = new Point(sp.x, sp.y);
SwingUtilities.convertPointFromScreen(cp, c);
if (!c.contains(cp.x, cp.y)) {
return c;
}
final int ncomponents = c.getComponentCount();
final Component component[] = c.getComponents();
for (int i = 0; i < ncomponents; i++) {
final Component comp = component[i];
final Point loc = comp.getLocation();
if ((comp.contains(cp.x - loc.x, cp.y - loc.y)) && (comp.isVisible() == true)) {
// found a component that intersects the point, see if there
// is a deeper possibility.
if (comp instanceof Container) {
final Container child = (Container) comp;
final Component deeper = findComponentAt(child, sp);
if (deeper != null) {
return deeper;
}
} else {
return comp;
}
}
}
return c;
}
protected Point getMouseLoc() {
// I observed a null PointerInfo after unplugging a second monitor
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo == null)
return null;
Point p = pointerInfo.getLocation();
SwingUtilities.convertPointFromScreen(p, zoomedComponent);
return p;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, container);
showBuildingsAction(location.x, location.y);
}
void createPopup(int xpos, int ypos, boolean contextMenu) {
if (manager == null) {
return;
}
if (!popupAllowed) {
return;
}
if (!isShowing()) {
return;
}
JPopupMenu popup;
if (contextMenu) {
popup = Utilities.actionsToPopup(manager.getExploredContext().getActions(true), this);
} else {
Action[] actions = NodeOp.findActions(manager.getSelectedNodes());
popup = Utilities.actionsToPopup(actions, this);
}
if ((popup != null) && (popup.getSubElements().length > 0)) {
Point p = getViewport().getViewPosition();
p.x = xpos - p.x;
p.y = ypos - p.y;
SwingUtilities.convertPointToScreen(p, ListView.this);
Dimension popupSize = popup.getPreferredSize();
Rectangle screenBounds = Utilities.getUsableScreenBounds(getGraphicsConfiguration());
if ((p.x + popupSize.width) > (screenBounds.x + screenBounds.width)) {
p.x = (screenBounds.x + screenBounds.width) - popupSize.width;
}
if ((p.y + popupSize.height) > (screenBounds.y + screenBounds.height)) {
p.y = (screenBounds.y + screenBounds.height) - popupSize.height;
}
SwingUtilities.convertPointFromScreen(p, ListView.this);
popup.show(this, p.x, p.y);
}
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, container);
showProductionBuildingsAction(location.x, location.y);
}
private boolean isPointWithinParentBounds(final Point pointInScreenCoordinates) {
final Point pointInParentCoordinates = new Point(pointInScreenCoordinates);
SwingUtilities.convertPointFromScreen(pointInParentCoordinates, parent);
return parent.getBounds().contains(pointInParentCoordinates);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, container);
showPlayerCharacterMenu(location.x, location.y);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, container);
showPlantAction(location.x, location.y);
}