下面列出了javax.swing.SwingConstants#NORTH_EAST 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Cursor getCursor() {
switch (position) {
case SwingConstants.NORTH:
return Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
case SwingConstants.SOUTH:
return Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
case SwingConstants.EAST:
return Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
case SwingConstants.WEST:
return Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
case SwingConstants.NORTH_EAST:
return Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
case SwingConstants.SOUTH_EAST:
return Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
case SwingConstants.NORTH_WEST:
return Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
case SwingConstants.SOUTH_WEST:
return Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
}
return Cursor.getDefaultCursor();
}
private Rectangle getRectangle(int x, int y, int w, int h, int location) {
switch (location) {
case SwingConstants.NORTH:
return new Rectangle(x + w / 2 - dist / 2, y, dist, dist);
case SwingConstants.SOUTH:
return new Rectangle(x + w / 2 - dist / 2, y + h - dist, dist, dist);
case SwingConstants.WEST:
return new Rectangle(x, y + h / 2 - dist / 2, dist, dist);
case SwingConstants.EAST:
return new Rectangle(x + w - dist, y + h / 2 - dist / 2, dist, dist);
case SwingConstants.NORTH_WEST:
return new Rectangle(x, y, dist, dist);
case SwingConstants.NORTH_EAST:
return new Rectangle(x + w - dist, y, dist, dist);
case SwingConstants.SOUTH_WEST:
return new Rectangle(x, y + h - dist, dist, dist);
case SwingConstants.SOUTH_EAST:
return new Rectangle(x + w - dist, y + h - dist, dist, dist);
}
return null;
}
private static void checkSupported(int constraint) {
if (constraint == SwingConstants.NORTH) return;
if (constraint == SwingConstants.WEST) return;
if (constraint == SwingConstants.SOUTH) return;
if (constraint == SwingConstants.EAST) return;
if (constraint == SwingConstants.NORTH_WEST) return;
if (constraint == SwingConstants.NORTH_EAST) return;
if (constraint == SwingConstants.SOUTH_WEST) return;
if (constraint == SwingConstants.SOUTH_EAST) return;
if (constraint == SwingConstants.CENTER) return;
throw new IllegalArgumentException("Unsupported constraint: " + constraint); // NOI18N
}
private static String constraintName(int constraint) {
if (constraint == SwingConstants.NORTH) return "NORTH"; // NOI18N
if (constraint == SwingConstants.WEST) return "WEST"; // NOI18N
if (constraint == SwingConstants.SOUTH) return "SOUTH"; // NOI18N
if (constraint == SwingConstants.EAST) return "EAST"; // NOI18N
if (constraint == SwingConstants.NORTH_WEST) return "NORTH_WEST"; // NOI18N
if (constraint == SwingConstants.NORTH_EAST) return "NORTH_EAST"; // NOI18N
if (constraint == SwingConstants.SOUTH_WEST) return "SOUTH_WEST"; // NOI18N
if (constraint == SwingConstants.SOUTH_EAST) return "SOUTH_EAST"; // NOI18N
if (constraint == SwingConstants.CENTER) return "CENTER"; // NOI18N
return "UNSUPPORTED_CONSTRAINT (value=" + constraint + ")"; // NOI18N
}
protected void displayPopup() {
JPopupMenu menu = new JPopupMenu();
populatePopup(menu);
if (menu.getComponentCount() > 0) {
Dimension size = menu.getPreferredSize();
size.width = Math.max(size.width, getWidth());
menu.setPreferredSize(size);
int align = getPopupAlign();
int x;
switch (align) {
case SwingConstants.EAST:
case SwingConstants.NORTH_EAST:
case SwingConstants.SOUTH_EAST:
x = getWidth() - size.width;
break;
default:
x = 0;
break;
}
int y;
switch (align) {
case SwingConstants.NORTH:
case SwingConstants.NORTH_EAST:
case SwingConstants.NORTH_WEST:
y = -size.height;
break;
default:
y = getHeight();
break;
}
menu.show(this, x, y);
}
}
private static void checkSupported(int constraint) {
if (constraint == SwingConstants.NORTH) return;
if (constraint == SwingConstants.WEST) return;
if (constraint == SwingConstants.SOUTH) return;
if (constraint == SwingConstants.EAST) return;
if (constraint == SwingConstants.NORTH_WEST) return;
if (constraint == SwingConstants.NORTH_EAST) return;
if (constraint == SwingConstants.SOUTH_WEST) return;
if (constraint == SwingConstants.SOUTH_EAST) return;
if (constraint == SwingConstants.CENTER) return;
throw new IllegalArgumentException("Unsupported constraint: " + constraint); // NOI18N
}
private static String constraintName(int constraint) {
if (constraint == SwingConstants.NORTH) return "NORTH"; // NOI18N
if (constraint == SwingConstants.WEST) return "WEST"; // NOI18N
if (constraint == SwingConstants.SOUTH) return "SOUTH"; // NOI18N
if (constraint == SwingConstants.EAST) return "EAST"; // NOI18N
if (constraint == SwingConstants.NORTH_WEST) return "NORTH_WEST"; // NOI18N
if (constraint == SwingConstants.NORTH_EAST) return "NORTH_EAST"; // NOI18N
if (constraint == SwingConstants.SOUTH_WEST) return "SOUTH_WEST"; // NOI18N
if (constraint == SwingConstants.SOUTH_EAST) return "SOUTH_EAST"; // NOI18N
if (constraint == SwingConstants.CENTER) return "CENTER"; // NOI18N
return "UNSUPPORTED_CONSTRAINT (value=" + constraint + ")"; // NOI18N
}
protected void displayPopup() {
JPopupMenu menu = new JPopupMenu();
populatePopup(menu);
if (menu.getComponentCount() > 0) {
Dimension size = menu.getPreferredSize();
size.width = Math.max(size.width, getWidth());
menu.setPreferredSize(size);
int align = getPopupAlign();
int x;
switch (align) {
case SwingConstants.EAST:
case SwingConstants.NORTH_EAST:
case SwingConstants.SOUTH_EAST:
x = getWidth() - size.width;
break;
default:
x = 0;
break;
}
int y;
switch (align) {
case SwingConstants.NORTH:
case SwingConstants.NORTH_EAST:
case SwingConstants.NORTH_WEST:
y = -size.height;
break;
default:
y = getHeight();
break;
}
menu.show(this, x, y);
}
}
int pointToSelectionArea(int px, int py) {
if(selectionBoundingBox == null) return -1;
int x = selectionBoundingBox.x;
int y = selectionBoundingBox.y;
int w = selectionBoundingBox.width;
int wh = w / 2;
int h = selectionBoundingBox.height;
int hh = h / 2;
if(px >= x - SELECTION_MARK_CENTER && px <= x + SELECTION_MARK_CENTER) {
if(py >= y - SELECTION_MARK_CENTER && py <= y + SELECTION_MARK_CENTER)
return SwingConstants.NORTH_WEST;
if(py >= y - SELECTION_MARK_CENTER + hh && py <= y + SELECTION_MARK_CENTER + hh)
return SwingConstants.WEST;
if(py >= y - SELECTION_MARK_CENTER + h && py <= y + SELECTION_MARK_CENTER + h)
return SwingConstants.SOUTH_WEST;
}
if(px >= x - SELECTION_MARK_CENTER + wh && px <= x + SELECTION_MARK_CENTER + wh) {
if(py >= y - SELECTION_MARK_CENTER && py <= y + SELECTION_MARK_CENTER)
return SwingConstants.NORTH;
if(py >= y - SELECTION_MARK_CENTER + h && py <= y + SELECTION_MARK_CENTER + h)
return SwingConstants.SOUTH;
}
if(px >= x - SELECTION_MARK_CENTER + w && px <= x + SELECTION_MARK_CENTER + w) {
if(py >= y - SELECTION_MARK_CENTER && py <= y + SELECTION_MARK_CENTER)
return SwingConstants.NORTH_EAST;
if(py >= y - SELECTION_MARK_CENTER + hh && py <= y + SELECTION_MARK_CENTER + hh)
return SwingConstants.EAST;
if(py >= y - SELECTION_MARK_CENTER + h && py <= y + SELECTION_MARK_CENTER + h)
return SwingConstants.SOUTH_EAST;
}
return selectionBoundingBox.contains(px, py) ? SwingConstants.CENTER : -1;
}
/**
* Updates cursor according to its location. For example, it sets
* the appropriate resizing cursor when the mouse is over the boundary
* of the selection component.
*
* @param cursorLocation current mouse cursor location.
*/
void updateCursor(Point cursorLocation) {
Cursor cursor = Cursor.getDefaultCursor();
if (cursorLocation == null) {
resizingMode = 0;
} else {
int x = cursorLocation.x;
int y = cursorLocation.y;
Image resizeHandle = GridDesigner.RESIZE_HANDLE;
int rw = resizeHandle.getWidth(null);
int rh = resizeHandle.getHeight(null);
for (Component selComp : selection) {
Rectangle rect = fromComponentPane(selectionResizingBounds(selComp));
boolean w = (rect.x-rw<=x) && (x<=rect.x+rect.width+rw);
boolean h = (rect.y-rh<=y) && (y<=rect.y+rect.height+rh);
boolean top = w && (rect.y-rh<=y) && (y<=rect.y+2);
boolean bottom = w && (rect.y+rect.height-2<=y) && (y<=rect.y+rect.height+rh);
boolean left = h && (rect.x-rw<=x) && (x<=rect.x+2);
boolean right = h && (rect.x+rect.width-2<=x) && (x<=rect.x+rect.width+rw);
if (top) {
if (left) {
cursor = Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
resizingMode = SwingConstants.NORTH_WEST;
} else if (right) {
cursor = Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
resizingMode = SwingConstants.NORTH_EAST;
} else {
cursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
resizingMode = SwingConstants.NORTH;
}
} else if (bottom) {
if (left) {
cursor = Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
resizingMode = SwingConstants.SOUTH_WEST;
} else if (right) {
cursor = Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
resizingMode = SwingConstants.SOUTH_EAST;
} else {
cursor = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
resizingMode = SwingConstants.SOUTH;
}
} else if (left) {
cursor = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
resizingMode = SwingConstants.WEST;
} else if (right) {
cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
resizingMode = SwingConstants.EAST;
} else {
cursor = Cursor.getDefaultCursor();
resizingMode = 0;
}
if (resizingMode != 0) {
focusedComponent = selComp;
break;
}
}
}
setCursor(cursor);
}
public void show() {
// Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
// if (focusOwner != null) focusRef = new WeakReference(focusOwner);
owner = ownerRef == null ? null : ownerRef.get();
ownerLocation = owner == null ? null : owner.getLocationOnScreen();
window = new JWindow(owner);
window.setType(Window.Type.POPUP);
window.setAlwaysOnTop(false);
window.setFocusable(true);
window.setFocusableWindowState(true);
window.setAutoRequestFocus(true);
window.getContentPane().add(content);
window.pack();
if (popupAlign == -1) {
window.setLocation(location.getLocation());
} else {
Dimension size = content.getSize();
int x;
switch (popupAlign) {
case SwingConstants.EAST:
case SwingConstants.NORTH_EAST:
case SwingConstants.SOUTH_EAST:
x = location.x + location.width - size.width + 1;
break;
default:
x = location.x + 1;
break;
}
int y;
switch (popupAlign) {
case SwingConstants.NORTH:
case SwingConstants.NORTH_EAST:
case SwingConstants.NORTH_WEST:
y = location.y - size.height + 1;
break;
default:
y = location.y + location.height + 1;
break;
}
window.setLocation(x, y);
}
window.setVisible(true);
Component defaultFocus = content.getFocusTraversalPolicy().getDefaultComponent(content);
if (defaultFocus != null) defaultFocus.requestFocusInWindow();
content.installListeners();
if (listener != null) listener.popupShown();
}
public void show() {
// Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
// if (focusOwner != null) focusRef = new WeakReference(focusOwner);
owner = ownerRef == null ? null : ownerRef.get();
ownerLocation = owner == null ? null : owner.getLocationOnScreen();
window = new JWindow(owner);
window.setType(Window.Type.POPUP);
window.setAlwaysOnTop(false);
window.setFocusable(true);
window.setFocusableWindowState(true);
window.setAutoRequestFocus(true);
window.getContentPane().add(content);
window.pack();
if (popupAlign == -1) {
window.setLocation(location.getLocation());
} else {
Dimension size = content.getSize();
int x;
switch (popupAlign) {
case SwingConstants.EAST:
case SwingConstants.NORTH_EAST:
case SwingConstants.SOUTH_EAST:
x = location.x + location.width - size.width + 1;
break;
default:
x = location.x + 1;
break;
}
int y;
switch (popupAlign) {
case SwingConstants.NORTH:
case SwingConstants.NORTH_EAST:
case SwingConstants.NORTH_WEST:
y = location.y - size.height + 1;
break;
default:
y = location.y + location.height + 1;
break;
}
window.setLocation(x, y);
}
window.setVisible(true);
Component defaultFocus = content.getFocusTraversalPolicy().getDefaultComponent(content);
if (defaultFocus != null) defaultFocus.requestFocusInWindow();
content.installListeners();
if (listener != null) listener.popupShown();
}
public void defineShape(Rectangle r) {
shape = new GeneralPath();
int k1 = 15;
int k2 = 8;
switch (position) {
case SwingConstants.NORTH_WEST:
shape.moveTo(r.x, r.y);
shape.lineTo(r.x + k1, r.y);
shape.lineTo(r.x + k1, r.y - k2);
shape.lineTo(r.x - k2, r.y - k2);
shape.lineTo(r.x - k2, r.y + k1);
shape.lineTo(r.x, r.y + k1);
shape.closePath();
break;
case SwingConstants.NORTH_EAST:
shape.moveTo(r.x + r.width, r.y);
shape.lineTo(r.x + r.width - k1, r.y);
shape.lineTo(r.x + r.width - k1, r.y - k2);
shape.lineTo(r.x + r.width + k2, r.y - k2);
shape.lineTo(r.x + r.width + k2, r.y + k1);
shape.lineTo(r.x + r.width, r.y + k1);
shape.closePath();
break;
case SwingConstants.SOUTH_WEST:
shape.moveTo(r.x, r.y + r.height);
shape.lineTo(r.x + k1, r.y + r.height);
shape.lineTo(r.x + k1, r.y + r.height + k2);
shape.lineTo(r.x - k2, r.y + r.height + k2);
shape.lineTo(r.x - k2, r.y + r.height - k1);
shape.lineTo(r.x, r.y + r.height - k1);
shape.closePath();
break;
case SwingConstants.SOUTH_EAST:
shape.moveTo(r.x + r.width, r.y + r.height);
shape.lineTo(r.x + r.width - k1, r.y + r.height);
shape.lineTo(r.x + r.width - k1, r.y + r.height + k2);
shape.lineTo(r.x + r.width + k2, r.y + r.height + k2);
shape.lineTo(r.x + r.width + k2, r.y + r.height - k1);
shape.lineTo(r.x + r.width, r.y + r.height - k1);
shape.closePath();
break;
case SwingConstants.NORTH:
shape.moveTo(r.x + r.width / 2 - k1 / 2, r.y);
shape.lineTo(r.x + r.width / 2 + k1 / 2, r.y);
shape.lineTo(r.x + r.width / 2 + k1 / 2, r.y - k2);
shape.lineTo(r.x + r.width / 2 - k1 / 2, r.y - k2);
break;
case SwingConstants.SOUTH:
shape.moveTo(r.x + r.width / 2 - k1 / 2, r.y + r.height);
shape.lineTo(r.x + r.width / 2 + k1 / 2, r.y + r.height);
shape.lineTo(r.x + r.width / 2 + k1 / 2, r.y + r.height
+ k2);
shape.lineTo(r.x + r.width / 2 - k1 / 2, r.y + r.height
+ k2);
break;
case SwingConstants.EAST:
shape.moveTo(r.x, r.y + r.height / 2 - k1 / 2);
shape.lineTo(r.x, r.y + r.height / 2 + k1 / 2);
shape.lineTo(r.x - k2, r.y + r.height / 2 + k1 / 2);
shape.lineTo(r.x - k2, r.y + r.height / 2 - k1 / 2);
break;
case SwingConstants.WEST:
shape.moveTo(r.x + r.width, r.y + r.height / 2 - k1 / 2);
shape.lineTo(r.x + r.width, r.y + r.height / 2 + k1 / 2);
shape.lineTo(r.x + r.width + k2, r.y + r.height / 2 + k1
/ 2);
shape.lineTo(r.x + r.width + k2, r.y + r.height / 2 - k1
/ 2);
break;
default:
throw new RuntimeException("unexpected position: "
+ position);
}
}
/**
* Determines if we are moving east side of the component during resizing.
*
* @return {@code true} is we are moving east side of the component,
* returns {@code false} otherwise.
*/
private boolean isResizingEastward() {
return (resizingMode == SwingConstants.EAST
|| resizingMode == SwingConstants.SOUTH_EAST
|| resizingMode == SwingConstants.NORTH_EAST);
}
/**
* Determines if we are moving north side of the component during resizing.
*
* @return {@code true} is we are moving north side of the component,
* returns {@code false} otherwise.
*/
private boolean isResizingNorthward() {
return (resizingMode == SwingConstants.NORTH
|| resizingMode == SwingConstants.NORTH_EAST
|| resizingMode == SwingConstants.NORTH_WEST);
}