public class MyGUIButtonDrag extends MyGUIButton { private int xoff, yoff; // Used to track mouse offset so that object isn't dragged from centre private int minAreax, minAreay, maxAreax, maxAreay; // constrain object's drag area /* Constructors */ MyGUIButtonDrag(int x, int y, int width, int height) { super(x, y, width, height); } MyGUIButtonDrag(int x, int y, PImage icon) { super(x, y, icon); } void clearDragArea() { setDragArea(0, 0, 0, 0); } void setDragArea(int minx, int miny, int maxx, int maxy) { minAreax = minx; minAreay = miny; maxAreax = maxx; maxAreay = maxy; } void mousePressed() { if(hasFocus()) { dragged = true; xoff = _parent.tmouseX - _x; yoff = _parent.tmouseY - _y; } } void mouseDragged() { if(dragged) { _x = _parent.tmouseX - xoff; _y = _parent.tmouseY - yoff; // This logic reads: If all these values are not 0 Then constrain the _x, _y position if(!(minAreax == 0 && minAreay == 0 && maxAreax == 0 && maxAreay == 0)) { _x = constrain(_x, minAreax, maxAreax); _y = constrain(_y, minAreay, maxAreay); } } } }