下面列出了java.awt.Cursor#NW_RESIZE_CURSOR 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public CropBox(PRectangle rect, View view) {
this.rect = rect;
upperLeft = new CropHandle("NW", Cursor.NW_RESIZE_CURSOR, view);
upper = new CropHandle("N", Cursor.N_RESIZE_CURSOR, view);
upperRight = new CropHandle("NE", Cursor.NE_RESIZE_CURSOR, view);
right = new CropHandle("E", Cursor.E_RESIZE_CURSOR, view);
left = new CropHandle("W", Cursor.W_RESIZE_CURSOR, view);
lowerLeft = new CropHandle("SW", Cursor.SW_RESIZE_CURSOR, view);
lower = new CropHandle("S", Cursor.S_RESIZE_CURSOR, view);
lowerRight = new CropHandle("SE", Cursor.SE_RESIZE_CURSOR, view);
handles = List.of(upperLeft, upperRight, lowerRight, lowerLeft,
right, upper, lower, left);
update(rect);
}
public static boolean isResizeMode(int cursorType) {
switch (cursorType) {
case Cursor.NW_RESIZE_CURSOR:
case Cursor.SE_RESIZE_CURSOR:
case Cursor.SW_RESIZE_CURSOR:
case Cursor.NE_RESIZE_CURSOR:
case Cursor.N_RESIZE_CURSOR:
case Cursor.S_RESIZE_CURSOR:
case Cursor.E_RESIZE_CURSOR:
case Cursor.W_RESIZE_CURSOR:
return true;
}
return false;
}
public void mouseDragged(MouseEvent e) {
Window w = (Window) e.getSource();
Point pt = e.getPoint();
if (isMovingWindow) {
Point eventLocationOnScreen = e.getLocationOnScreen();
w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
} else if (dragCursor != 0) {
Rectangle r = w.getBounds();
Rectangle startBounds = new Rectangle(r);
Dimension min = w.getMinimumSize();
switch (dragCursor) {
case Cursor.E_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
break;
case Cursor.S_RESIZE_CURSOR:
adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.N_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
break;
case Cursor.W_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
break;
case Cursor.NE_RESIZE_CURSOR:
adjust(
r,
min,
0,
pt.y - dragOffsetY,
pt.x + (dragWidth - dragOffsetX) - r.width,
-(pt.y - dragOffsetY));
break;
case Cursor.SE_RESIZE_CURSOR:
adjust(
r,
min,
0,
0,
pt.x + (dragWidth - dragOffsetX) - r.width,
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.NW_RESIZE_CURSOR:
adjust(
r,
min,
pt.x - dragOffsetX,
pt.y - dragOffsetY,
-(pt.x - dragOffsetX),
-(pt.y - dragOffsetY));
break;
case Cursor.SW_RESIZE_CURSOR:
adjust(
r,
min,
pt.x - dragOffsetX,
0,
-(pt.x - dragOffsetX),
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
default:
break;
}
if (!r.equals(startBounds)) {
w.setBounds(r);
// Defer repaint/validate on mouseReleased unless dynamic
// layout is active.
if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
w.validate();
getRootPane().repaint();
}
}
}
}
private int getCursorType (Rectangle b, Point p) {
int leftDist = p.x - b.x;
int rightDist = (b.x + b.width) - p.x;
int topDist = p.y - b.y;
int bottomDist = (b.y + b.height) - p.y;
boolean isNearTop = topDist >= 0 && topDist <= insets.top;
boolean isNearBottom = bottomDist >= 0 && bottomDist <= insets.bottom;
boolean isNearLeft = leftDist >= 0 && leftDist <= insets.left;
boolean isNearRight = rightDist >= 0 && rightDist <= insets.right;
boolean isInTopPart = topDist >= 0 && topDist <= insets.top + 10;
boolean isInBottomPart = bottomDist >= 0 && bottomDist <= insets.bottom + 10;
boolean isInLeftPart = leftDist >= 0 && leftDist <= insets.left + 10;
boolean isInRightPart = rightDist >= 0 && rightDist <= insets.right + 10;
if (isNearTop && isInLeftPart || isInTopPart && isNearLeft) {
return Cursor.NW_RESIZE_CURSOR;
}
if (isNearTop && isInRightPart || isInTopPart && isNearRight) {
return Cursor.NE_RESIZE_CURSOR;
}
if (isNearBottom && isInLeftPart || isInBottomPart && isNearLeft) {
return Cursor.SW_RESIZE_CURSOR;
}
if (isNearBottom && isInRightPart || isInBottomPart && isNearRight) {
return Cursor.SE_RESIZE_CURSOR;
}
if (isNearTop) {
return Cursor.N_RESIZE_CURSOR;
}
if (isNearLeft) {
return Cursor.W_RESIZE_CURSOR;
}
if (isNearRight) {
return Cursor.E_RESIZE_CURSOR;
}
if (isNearBottom) {
return Cursor.S_RESIZE_CURSOR;
}
return Cursor.DEFAULT_CURSOR;
}
private Rectangle computeNewBounds (Window w, Point dragLoc) {
if (startDragLoc == null) {
throw new IllegalArgumentException("Can't compute bounds when startDragLoc is null"); //NOI18N
}
int xDiff = dragLoc.x - startDragLoc.x;
int yDiff = dragLoc.y - startDragLoc.y;
resizedBounds.setBounds(startWinBounds);
switch (cursorType) {
case Cursor.E_RESIZE_CURSOR:
resizedBounds.width = startWinBounds.width + (dragLoc.x - startDragLoc.x);
break;
case Cursor.W_RESIZE_CURSOR:
resizedBounds.width = startWinBounds.width - xDiff;
resizedBounds.x = startWinBounds.x + xDiff;
break;
case Cursor.N_RESIZE_CURSOR:
resizedBounds.height = startWinBounds.height - yDiff;
resizedBounds.y = startWinBounds.y + yDiff;
break;
case Cursor.S_RESIZE_CURSOR:
resizedBounds.height = startWinBounds.height + (dragLoc.y - startDragLoc.y);
break;
case Cursor.NE_RESIZE_CURSOR:
resize(resizedBounds, 0, yDiff, xDiff, -yDiff, minSize);
break;
case Cursor.NW_RESIZE_CURSOR:
resize(resizedBounds, xDiff, yDiff, -xDiff, -yDiff, minSize);
break;
case Cursor.SE_RESIZE_CURSOR:
resize(resizedBounds, 0, 0, xDiff, yDiff, minSize);
break;
case Cursor.SW_RESIZE_CURSOR:
resize(resizedBounds, xDiff, 0, -xDiff, yDiff, minSize);
break;
default:
System.out.println("unknown cursor type : " + cursorType);
//throw new IllegalArgumentException("Unknown/illegal cursor type: " + cursorType); //NOI18N
break;
}
return resizedBounds;
}
public void mouseMoved(Point point) {
synchronized (mouseLock) {
if (mousePressedPosition != null)
mouseDragPosition = point;
Bounds bounds = getSelected(point);
int cursorType;
if (bounds == null)
cursorType = Cursor.DEFAULT_CURSOR;
else {
cursorType = Cursor.MOVE_CURSOR;
}
if (selection.getBounds().length == 1) {
boolean resizableX = selection.isResizeableX(diagram);
boolean resizableY = selection.isResizeableY(diagram);
if ((resizableX) && (isRightMove(point)))
cursorType = Cursor.E_RESIZE_CURSOR;
else if ((resizableX) && (isLeftMove(point)))
cursorType = Cursor.W_RESIZE_CURSOR;
else if ((resizableY) && (isTopMove(point)))
cursorType = Cursor.N_RESIZE_CURSOR;
else if ((resizableY) && (isBottomMove(point)))
cursorType = Cursor.S_RESIZE_CURSOR;
else if ((resizableY) && (resizableX)
&& (isRightBottomMove(point)))
cursorType = Cursor.SE_RESIZE_CURSOR;
else if ((resizableY) && (resizableX)
&& (isLeftBottomMove(point)))
cursorType = Cursor.SW_RESIZE_CURSOR;
else if ((resizableY) && (resizableX)
&& (isTopRightMove(point)))
cursorType = Cursor.NE_RESIZE_CURSOR;
else if ((resizableY) && (resizableX) && (isTopLeftMove(point)))
cursorType = Cursor.NW_RESIZE_CURSOR;
}
if (this.cursorType != cursorType) {
this.cursorType = cursorType;
this.setCursor(new Cursor(cursorType));
}
}
repaint();
}
public int getCursor(int w, int h, Point point, Insets inset)
{
int radius = -1;
//
int startX = inset.left + radius;
int endX = w - inset.right + radius;
int startY = inset.top + radius;
int endY = h - inset.bottom + radius;
if (point.x <= startX && point.y <= startY)
{
// 左上角
return Cursor.NW_RESIZE_CURSOR;
}
else if (point.x >= endX && point.y <= startY)
{
// 右上角
return Cursor.NE_RESIZE_CURSOR;
}
else if (point.x <= startX && point.y >= endY)
{
// 左下
return Cursor.SW_RESIZE_CURSOR;
}
else if(point.x >= endX && point.y >= endY)
{
// 右下
return Cursor.SE_RESIZE_CURSOR;
}
else if(point.x <= startX && point.y > startY && point.y < endY)
{
// 西
return Cursor.W_RESIZE_CURSOR;
}
else if(point.y <= startY && point.x > startX && point.x < endX)
{
// 北
return Cursor.N_RESIZE_CURSOR;
}
else if(point.x >= endX && point.y > startY && point.y < endY)
{
// 东
return Cursor.E_RESIZE_CURSOR;
}
else if(point.y >= endY && point.x > startX && point.x < endX)
{
// 南
return Cursor.S_RESIZE_CURSOR;
}
else
{
return Cursor.DEFAULT_CURSOR;
}
}
/**
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent ev) {
Window w = (Window) ev.getSource();
Point pt = ev.getPoint();
if (isMovingWindow) {
Point eventLocationOnScreen = ev.getLocationOnScreen();
w.setLocation(eventLocationOnScreen.x - dragOffsetX, eventLocationOnScreen.y - dragOffsetY);
} else if (dragCursor != 0) {
Rectangle r = w.getBounds();
Rectangle startBounds = new Rectangle(r);
Dimension min = w.getMinimumSize();
switch (dragCursor) {
case Cursor.E_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
break;
case Cursor.S_RESIZE_CURSOR:
adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.N_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
break;
case Cursor.W_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
break;
case Cursor.NE_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX) - r.width, -(pt.y - dragOffsetY));
break;
case Cursor.SE_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.NW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, pt.y - dragOffsetY, -(pt.x - dragOffsetX), -(pt.y - dragOffsetY));
break;
case Cursor.SW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), pt.y + (dragHeight - dragOffsetY) - r.height);
break;
default:
break;
}
if (!r.equals(startBounds)) {
w.setBounds(r);
// Defer repaint/validate on mouseReleased unless dynamic
// layout is active.
if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
w.validate();
getRootPane().repaint();
}
}
}
}
/**
* Recalculate rect position and size, according to mouse offset and direction
*
* @param rect rectangle to adjust
* @param cursorType user modification direction (N,S,W,E,NW,NE,SE,SW)
* @param moveOffset offset must be calculated as: mousePos - mouseStartPos
*/
public static void resize(Rectangle rect, int cursorType, Point moveOffset) {
int offsetX = moveOffset.x;
int offsetY = moveOffset.y;
switch (cursorType) {
case Cursor.NW_RESIZE_CURSOR:
rect.width -= offsetX;
rect.height -= offsetY;
rect.x += offsetX;
rect.y += offsetY;
break;
case Cursor.SE_RESIZE_CURSOR:
rect.width += offsetX;
rect.height += offsetY;
break;
case Cursor.SW_RESIZE_CURSOR:
rect.width -= offsetX;
rect.height += offsetY;
rect.x += offsetX;
break;
case Cursor.NE_RESIZE_CURSOR:
rect.width += offsetX;
rect.height -= offsetY;
rect.y += offsetY;
break;
case Cursor.N_RESIZE_CURSOR:
rect.height -= offsetY;
rect.y += offsetY;
break;
case Cursor.S_RESIZE_CURSOR:
rect.height += offsetY;
break;
case Cursor.E_RESIZE_CURSOR:
rect.width += offsetX;
break;
case Cursor.W_RESIZE_CURSOR:
rect.width -= offsetX;
rect.x += offsetX;
break;
}
}
public void updateBound(Point pt, Window w)
{
Rectangle r = w.getBounds();
Rectangle startBounds = new Rectangle(r);
Dimension min = w.getMinimumSize();
switch (dragCursor)
{
case Cursor.E_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
break;
case Cursor.S_RESIZE_CURSOR:
adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.N_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
break;
case Cursor.W_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
break;
case Cursor.NE_RESIZE_CURSOR:
adjust(r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX)
- r.width, -(pt.y - dragOffsetY));
break;
case Cursor.SE_RESIZE_CURSOR:
adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width,
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
case Cursor.NW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, pt.y - dragOffsetY,
-(pt.x - dragOffsetX), -(pt.y - dragOffsetY));
break;
case Cursor.SW_RESIZE_CURSOR:
adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX),
pt.y + (dragHeight - dragOffsetY) - r.height);
break;
default:
break;
}
if(!r.equals(startBounds))
{
w.setBounds(r);
if (Toolkit.getDefaultToolkit().isDynamicLayoutActive())
{
w.validate();
w.repaint();
}
}
}