Libraryless. Click here for Pure Java version (5239L/29K).
// 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; // if != null, we're engaged JComponent src; Window window; Rect originalPosition, srcBounds; int dragX, dragY; // -1, 0 or 1 depending on what we are dragging Cursor originalCursor, ourCursor; *(JComponent *src) { addMouseAndMotionListener(src, this); } bool engaged() { ret mouseDownEvent != null; } void start(MouseEvent e) { this.mouseDownEvent = e; window = getWindow(src); if (window == null) ret; originalPosition = toRect(getBounds(window)); 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 (window cast JFrame && isMaximized(window)) ret; if (engaged()) ret; // what? 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 (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(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 {} }
Began life as a copy of #1033463
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033471 |
Snippet name: | WindowResizeDragger - resize undecorated JFrame with mouse |
Eternal ID of this version: | #1033471/40 |
Text MD5: | 825aa2b2c71568086841e736b46b814f |
Transpilation MD5: | 0367df3596c3ec64fe5fc769c2ef6ac8 |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-05 07:03:14 |
Source code size: | 4109 bytes / 132 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 225 / 356 |
Version history: | 39 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) #1035241 - ComponentResizeDragger - resize a GUI Component with mouse |