You can drag out a component, even from a window, perfectly designed to support drag and drop.
For drag and drop events, you just need to limit the location of the event inside the container:
static public final int MAX_X=(CANVAS_WIDTH - PADDLE_WIDTH);
...
public void mouseDragged(MouseEvent e) {
int px=Math.max(0,Math.min(e.getX(),MAX_X));
int dx=(px-lastX);
if(dx!=0) {
gobj.move(dx);
lastX=px;
}
}
, - , GObject, , . .
, (, /) . , - . , x = 350, 700-150 = 550, 350-550 = -200, 200 .
, 0 <= px <= UPPER... - :
...
if(dx!=0) {
gobj.moveX(dx,MAX_X);
lastX=px;
}
...
GObject:
public void moveX(int dlt, int max) {
positionX=Util.limitInt((positionX+dlt),0,max);
}
Util:
static public int limitInt(int val, int low, int hgh) {
return ((val<low) ? low : ((val<hgh) ? val : hgh));
}
Y . :
public void move(int dltX, int maxX, int dltY, int maxY) {
positionX=Util.limitInt((positionX+dltX),0,maxX);
positionY=Util.limitInt((positionX+dltY),0,maxY);
}