List of usage examples for java.awt.dnd DragGestureEvent startDrag
public void startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl) throws InvalidDnDOperationException
From source file:ColorSource.java
public void dragGestureRecognized(DragGestureEvent e) { // Create an image we can drag along with us. // Not all systems support this, but it doesn't hurt to try. Image colorblock = this.createImage(25, 25); Graphics g = colorblock.getGraphics(); g.setColor(color);//from w w w . j a v a 2 s.co m g.fillRect(0, 0, 25, 25); // Start dragging our transferable color object. e.startDrag(DragSource.DefaultMoveDrop, // The initial drag cursor colorblock, new Point(0, 0), // The image to drag tcolor, // The data being dragged this); // Who to notify during drag }
From source file:ScribbleDragAndDrop.java
/** * This method implements the DragGestureListener interface. It will be * invoked when the DragGestureRecognizer thinks that the user has initiated * a drag. If we're not in drawing mode, then this method will try to figure * out which Scribble object is being dragged, and will initiate a drag on * that object.//from w ww . jav a 2 s. c o m */ public void dragGestureRecognized(DragGestureEvent e) { // Don't drag if we're not in drag mode if (!dragMode) return; // Figure out where the drag started MouseEvent inputEvent = (MouseEvent) e.getTriggerEvent(); int x = inputEvent.getX(); int y = inputEvent.getY(); // Figure out which scribble was clicked on, if any by creating a // small rectangle around the point and testing for intersection. Rectangle r = new Rectangle(x - LINEWIDTH, y - LINEWIDTH, LINEWIDTH * 2, LINEWIDTH * 2); int numScribbles = scribbles.size(); for (int i = 0; i < numScribbles; i++) { // Loop through the scribbles Scribble s = (Scribble) scribbles.get(i); if (s.intersects(r)) { // The user started the drag on top of this scribble, so // start to drag it. // First, remember which scribble is being dragged, so we can // delete it later (if this is a move rather than a copy) beingDragged = s; // Next, create a copy that will be the one dragged Scribble dragScribble = (Scribble) s.clone(); // Adjust the origin to the point the user clicked on. dragScribble.translate(-x, -y); // Choose a cursor based on the type of drag the user initiated Cursor cursor; switch (e.getDragAction()) { case DnDConstants.ACTION_COPY: cursor = DragSource.DefaultCopyDrop; break; case DnDConstants.ACTION_MOVE: cursor = DragSource.DefaultMoveDrop; break; default: return; // We only support move and copys } // Some systems allow us to drag an image along with the // cursor. If so, create an image of the scribble to drag if (dragSource.isDragImageSupported()) { Rectangle scribbleBox = dragScribble.getBounds(); Image dragImage = this.createImage(scribbleBox.width, scribbleBox.height); Graphics2D g = (Graphics2D) dragImage.getGraphics(); g.setColor(new Color(0, 0, 0, 0)); // transparent background g.fillRect(0, 0, scribbleBox.width, scribbleBox.height); g.setColor(Color.black); g.setStroke(linestyle); g.translate(-scribbleBox.x, -scribbleBox.y); g.draw(dragScribble); Point hotspot = new Point(-scribbleBox.x, -scribbleBox.y); // Now start dragging, using the image. e.startDrag(cursor, dragImage, hotspot, dragScribble, this); } else { // Or start the drag without an image e.startDrag(cursor, dragScribble, this); } // After we've started dragging one scribble, stop looking return; } } }