// This class uses the border of the component that caught // the mouse event to determine the drag direction. sclass WindowResizeDragger extends MouseAdapter { int cornerSize = 10; // how big do we assume the corners are bool debug; Pt minWindowSize = pt(20, 20); MouseEvent mouseDownEvent; JComponent src; Window window; Rect originalPosition, srcBounds; int dragX, dragY; // -1, 0 or 1 depending on what we are dragging Cursor originalCursor; *(MouseEvent *mouseDownEvent) { if (mouseDownEvent == null) ret; src = optCast JComponent(mouseDownEvent.getComponent()); if (src == null) ret; window = getWindow(src); if (window == null) ret; srcBounds = toRect(boundsOnScreen(src)); originalPosition = toRect(getBounds(window)); Pt mouse = pt(mouseDownEvent.getXOnScreen(), mouseDownEvent.getYOnScreen()); if (distance(mouse.y, srcBounds.y2()) <= cornerSize) dragY = 1; else if (distance(mouse.y, srcBounds.y1()) <= cornerSize) dragY = -1; if (distance(mouse.x, srcBounds.x2()) <= cornerSize) dragX = 1; else if (distance(mouse.x, srcBounds.x1()) <= cornerSize) dragX = -1; if (dragX == 0 && dragY == 0) ret; originalCursor = src.getCursor(); Cursor cursor = new Cursor[] { Cursor.NW_RESIZE_CURSOR, Cursor.N_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR, null, Cursor.E_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR, }[dragY*3+dragX+4]; window.setCursor(cursor); if (debug) printVars("WindowResizeDragger", +dragX, +dragY, +mouse, +srcBounds, +cornerSize); addMouseAndMotionListener(src, this); } public void mouseDragged(MouseEvent e) { updatePosition(e); } public void mouseReleased(MouseEvent e) { if (e.getButton() != mouseDownEvent.getButton()) ret; updatePosition(e); if (debug) print("drag done"); removeMouseAdapter(src, this); dragDone(); } void updatePosition(MouseEvent e) { //if (debug) printVars("updatePosition", +e); Rect w = toRect(window.getBounds()); Pt a = topLeftCorner(w), b = bottomRightCorner(w); Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen()); if (dragX < 0) a.x = min(b.x-minWindowSize.x, mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen()); else if (dragX > 0) b.x = max(a.x+minWindowSize.x, mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen()); if (dragY < 0) a.y = min(b.y-minWindowSize.y, mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen()); else if (dragY > 0) b.y = max(a.y+minWindowSize.y, mouse.y + originalPosition.y2() - mouseDownEvent.getYOnScreen()); Rect r = rectFromPoints(a, b); if (debug) printVars("updatePosition", +w, +mouse, +a, +b, +dragX, +dragY); window.setBounds(toRectangle(r)); } void dragDone {} }