
Example 6−2: ColorSour ce.java (continued)
Image colorblock = this.createImage(25,25);
Graphics g = colorblock.getGraphics();
g.setColor(color);
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
}
// These methods implement DragSourceListener.
// Since we passed this object to startDrag, these methods will be
// called at interesting points during the drag. We could use them,
// for example, to implement custom cursors or other "drag-over" effects.
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dragDropEnd(DragSourceDropEvent e) {}
public void dragOver(DragSourceDragEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
}
A Data Sink
Example 6-3 shows the ColorSink class, which is a simple subclass of the Swing
JTextArea class. ColorSink allows color objects to be pasted or dropped on it.
When either event occurs,
ColorSink sets its background color to the transferred
color object. In addition, the class allows the pasting of textual data, which it
inserts at the current cursor position. Finally,
ColorSink accepts drops of the
DataFlavor.javaFileListFlavor type. This data flavor is used when the user
drags and drops a file icon. When ...