コード例 #1
0
 public void mouseDragged(MouseEvent evt) {
   // User has moved the mouse.  Move the dragged shape by the same amount.
   int x = evt.getX();
   int y = evt.getY();
   if (shapeBeingDragged != null) {
     shapeBeingDragged.moveBy(x - prevDragX, y - prevDragY);
     prevDragX = x;
     prevDragY = y;
     repaint(); // redraw canvas to show shape in new position
   }
 }
コード例 #2
0
 public void mouseReleased(MouseEvent evt) {
   // User has released the mouse.  Move the dragged shape, then set
   // shapeBeingDragged to null to indicate that dragging is over.
   // If the shape lies completely outside the canvas, remove it
   // from the list of shapes (since there is no way to ever move
   // it back onscreen).
   int x = evt.getX();
   int y = evt.getY();
   if (shapeBeingDragged != null) {
     shapeBeingDragged.moveBy(x - prevDragX, y - prevDragY);
     if (shapeBeingDragged.left >= getSize().width
         || shapeBeingDragged.top >= getSize().height
         || shapeBeingDragged.left + shapeBeingDragged.width < 0
         || shapeBeingDragged.top + shapeBeingDragged.height < 0) { // shape is off-screen
       shapes.remove(shapeBeingDragged); // remove shape from list of shapes
     }
     shapeBeingDragged = null;
     repaint();
   }
 }