FileDocCategorySizeDatePackage
ColorSource.javaAPI DocExample5365Mon Oct 11 15:07:22 BST 1999None

ColorSource

public class ColorSource extends JComponent implements DragGestureListener, ClipboardOwner, DragSourceListener
This 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
defaultBorder
A ColorSource normally displays itself with this border
protected static Border
highlightBorder
When 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 voidcopy()
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 voiddragDropEnd(java.awt.dnd.DragSourceDropEvent e)

public voiddragEnter(java.awt.dnd.DragSourceDragEvent e)

public voiddragExit(java.awt.dnd.DragSourceEvent e)

public voiddragGestureRecognized(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 voiddragOver(java.awt.dnd.DragSourceDragEvent e)

public voiddropActionChanged(java.awt.dnd.DragSourceDragEvent e)

public java.awt.DimensiongetMinimumSize()

      return mysize; 
public java.awt.DimensiongetPreferredSize()

 return mysize; 
public voidlostOwnership(java.awt.datatransfer.Clipboard clipboard, java.awt.datatransfer.Transferable contents)

    this.setBorder(defaultBorder);
  
public voidpaintComponent(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);