// This class uses the border of the component that caught // the mouse event to determine the drag direction. sclass ComponentResizeDragger extends MouseAdapter { Component component; // component to resize int cornerSize = 10; // how big do we assume the corners are settable bool debug; MouseEvent mouseDownEvent; // if != null, we're engaged JComponent src; Rect originalPosition, srcBounds; int dragX, dragY; // -1, 0 or 1 depending on what we are dragging Cursor originalCursor, ourCursor; *(Component *component, JComponent *src) { addMouseAndMotionListener(src, this); } bool engaged() { ret mouseDownEvent != null; } void start(MouseEvent e) { this.mouseDownEvent = e; originalPosition = toRect(getBounds(component)); cursorCalc(e, true); } void cursorCalc(MouseEvent e, bool inside) { srcBounds = toRect(boundsOnScreen(src)); Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen()); if (distance(mouse.y, srcBounds.y2()) <= cornerSize) dragY = 1; else if (distance(mouse.y, srcBounds.y1()) <= cornerSize) dragY = -1; else dragY = 0; if (distance(mouse.x, srcBounds.x2()) <= cornerSize) dragX = 1; else if (distance(mouse.x, srcBounds.x1()) <= cornerSize) dragX = -1; else dragX = 0; originalCursor = src.getCursor(); int iCursor = dragY*3+dragX+4; int cursor = new int[] { Cursor.NW_RESIZE_CURSOR, Cursor.N_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR, -1, Cursor.E_RESIZE_CURSOR, Cursor.SW_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR, }[iCursor]; Cursor newCursor = cursor >= 0 && inside ? Cursor.getPredefinedCursor(cursor) : null; //if (debug) printVars(iCursor, +ourCursor, +newCursor); if (newCursor != ourCursor) { ourCursor = newCursor; src.setCursor(newCursor); } /*if (debug) printVars("WindowResizeDragger", +dragX, +dragY, +mouse, +srcBounds, +cornerSize);*/ } @Override public void mouseEntered(MouseEvent e) { if (debug) print("mouseEntered"); if (!engaged()) cursorCalc(e, true); } @Override public void mouseExited(MouseEvent e) { if (debug) print("mouseExited"); if (!engaged()) cursorCalc(e, false); } public void mousePressed(MouseEvent e) { if (e.getButton() != MouseEvent.BUTTON1) ret; //if (component cast JFrame && isMaximized(component)) ret; if (engaged()) ret; // what? if (!mouseEventIsInBorder(e)) ret; start(e); } public void mouseDragged(MouseEvent e) { if (!engaged()) ret; updatePosition(e); } public void mouseMoved(MouseEvent e) { cursorCalc(e, true); } public void mouseReleased(MouseEvent e) { if (mouseDownEvent == null) ret; if (e.getButton() != mouseDownEvent.getButton()) ret; updatePosition(e); if (debug) print("drag done"); if (src.getCursor() == ourCursor) src.setCursor(originalCursor); mouseDownEvent = null; dragDone(); } void updatePosition(MouseEvent e) { //if (debug) printVars("updatePosition", +e); Rect w = toRect(component.getBounds()); Pt a = topLeftCorner(w), b = bottomRightCorner(w); Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen()); Pt minSize = toPt(component.getMinimumSize()); if (dragX < 0) a.x = max(0, min(b.x-minSize.x, mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen())); else if (dragX > 0) b.x = max(a.x+minSize.x, mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen()); if (dragY < 0) a.y = max(0, min(b.y-minSize.y, mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen())); else if (dragY > 0) b.y = max(a.y+minSize.y, mouse.y + originalPosition.y2() - mouseDownEvent.getYOnScreen()); Rect r = rectFromPoints(a, b); if (debug) printVars("ComponentResizeDragger.updatePosition", +w, +mouse, +a, +b, +dragX, +dragY); component.setBounds(toRectangle(r)); } void dragDone {} }