ColorSourcepublic class ColorSource extends JComponent implements DragGestureListener, ClipboardOwner, DragSourceListenerThis simple component displays a solid color, and allows that color
to be dragged. Also, it copies the color to the clipboard when the
user clicks on it. |
Fields Summary |
---|
Color | color | TransferableColor | tcolor | DragSource | dragSource | protected static Border | defaultBorderA ColorSource normally displays itself with this border | protected static Border | highlightBorderWhen we are the clipboard owner, uses this border | protected static Dimension | mysize |
Constructors Summary |
---|
public ColorSource(Color color)Create a new ColorSource object that displays the speciifed color
// Save the color. Encapsulate it in a Transferable object so that
// it can be used with cut-and-paste and drag-and-drop
this.color = color;
this.tcolor = new TransferableColor(color);
// Set our default border
this.setBorder(defaultBorder);
// Listen for mouse clicks, and copy the color to the clipboard.
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { copy(); }
});
// Set up a DragGestureRecognizer that will detect when the user
// begins a drag. When it detects one, it will notify us by calling
// the dragGestureRecognized() method of the DragGestureListener
// interface we implement below
this.dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, // Look for drags on us
DnDConstants.ACTION_COPY_OR_MOVE, // Recognize these types
this); // Tell us when recognized
|
Methods Summary |
---|
public void | copy()This method copies the color to the clipboard.
// Get system clipboard
Clipboard c = this.getToolkit().getSystemClipboard();
// Put our TransferableColor object on the clipboard.
// Also, we'll get notification when we no longer own the clipboard
c.setContents(tcolor, this);
// Set a special border on ourselves that indicates that we're the
// current color available for pasting.
this.setBorder(highlightBorder);
| public void | dragDropEnd(java.awt.dnd.DragSourceDropEvent e)
| public void | dragEnter(java.awt.dnd.DragSourceDragEvent e)
| public void | dragExit(java.awt.dnd.DragSourceEvent e)
| public void | dragGestureRecognized(java.awt.dnd.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);
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
| public void | dragOver(java.awt.dnd.DragSourceDragEvent e)
| public void | dropActionChanged(java.awt.dnd.DragSourceDragEvent e)
| public java.awt.Dimension | getMinimumSize()
return mysize;
| public java.awt.Dimension | getPreferredSize() return mysize;
| public void | lostOwnership(java.awt.datatransfer.Clipboard clipboard, java.awt.datatransfer.Transferable contents)
this.setBorder(defaultBorder);
| public void | paintComponent(java.awt.Graphics g)
g.setColor(color);
Dimension s = this.getSize();
Insets i = this.getInsets();
g.fillRect(i.left, i.top,
s.width-i.left-i.right, s.height-i.top-i.bottom);
|
|