// 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 = true; MouseEvent mouseDownEvent; JComponent src; Window window; Rect originalPosition, srcBounds; int dragX, dragY; // -1, 0 or 1 depending on what we are dragging *(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 (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); removeMouseAdapter(window, 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(mouseDownEvent.getXOnScreen(), mouseDownEvent.getYOnScreen()); if (dragX < 0) a.x = mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen(); else if (dragX > 0) b.x = mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen(); if (dragY < 0) a.y = mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen(); else if (dragY > 0) b.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 {} }