Java Q&A 
by Jason W. Purdy

Listing One
//DragSrc.java
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;

public class DragSrc extends Component implements DragSourceListener {
    private Color m_clrColor = Color.red;
    private boolean m_bAddedYet = false;
    public DragSrc() {
        this(Color.red);
    }
    public DragSrc(Color c) {
        super();
        m_clrColor = c;
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }
    public void addNotify() {
        super.addNotify();
        m_bAddedYet = true;
    }
    public void paint(Graphics g) {
        if (m_bAddedYet) {
            Dimension d = getSize();
            g.setColor(m_clrColor);
            g.fill3DRect(0, 0, d.width, d.height, true);
        }
    }
    public void processMouseEvent(MouseEvent e) {
        if (e.getID() == MouseEvent.MOUSE_PRESSED) {
            DragSource ds = DragSource.getDefaultDragSource();
            String str = ":" + m_clrColor.getRed() + ";";
            str += m_clrColor.getGreen() + ";";
            str += m_clrColor.getBlue();
            StringSelection t = new StringSelection(str);
            ds.startDrag(this, e, DnDConstants.ACTION_COPY, 
                       DragSource.DefaultCopyDrop, null, null, t, this);
        }
    }
    public void dragDropEnd(DragSourceDropEvent dsde) { ; }
    public void dragEnter(DragSourceDragEvent dsde) { ; }
    public void dragExit(DragSourceEvent dsde) { ; }
    public void dragOver(DragSourceDragEvent dsde) { ; }
    public void dropActionChanged(DragSourceDragEvent dsde) { ; }
}


Listing Two
/* ColoredCanvas.java
 * This handles drop events, specificially when
 * Colors are dropped in.
*/

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.io.*;

public class ColoredCanvas extends Component implements DropTargetListener {
    private boolean m_bAddedYet = false;
    private Color m_clrColor = Color.white;
    private DataFlavor ccDataFlavor = DataFlavor.stringFlavor;
 
    public ColoredCanvas() {
        this(Color.white);
    }
    public ColoredCanvas(Color c) {
        super();
        
        setDropTarget(new DropTarget(this, this));
        m_clrColor = c;
    }
    public void paint(Graphics g) {
        if (m_bAddedYet) {
            Dimension d = getSize();
            String str = "Color Me!";
            int x = d.width/2 - (g.getFontMetrics()).stringWidth(str)/2;
            int y = d.height/2 - (g.getFontMetrics()).getHeight();
            g.setColor(m_clrColor);
            g.fillRect(0, 0, d.width, d.height);
            g.setColor(Color.black);
            g.drawString(str, x, y);
        }
    }
    public void addNotify() {
        super.addNotify();
        m_bAddedYet = true;
    }
    public void dragEnter(DropTargetDragEvent dtde) {
        DataFlavor df[] = dtde.getCurrentDataFlavors();
        for (int i=0;i<df.length;i++) {
            if (df[i].equals(ccDataFlavor)) {
                dtde.acceptDrag(DnDConstants.ACTION_COPY);
                return;
            }
        }
        dtde.rejectDrag();
    }
    public void dragExit(DropTargetEvent dte) { ; }
    public void dragOver(DropTargetDragEvent dtde) { ; }

    private Color parseColor (String color) {
        color = color.substring(color.indexOf(":")+1);
        int r, g, b;
        int index1 = color.indexOf(";");
        r = Integer.parseInt(color.substring(0, index1));
        int index2 = color.indexOf(";", index1+1);
        g = Integer.parseInt(color.substring(index1+1, index2));
        b = Integer.parseInt(color.substring(index2+1));
        return new Color(r,g,b);
    }
    public void drop(DropTargetDropEvent dtde) {
        DropTargetContext dtc = dtde.getDropTargetContext();
        Transferable t = dtde.getTransferable();
        if (!t.isDataFlavorSupported(ccDataFlavor)) {
            dtc.dropComplete(false);
            return;
       }
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Object obj;
        Color theNewColor = null;
        try {
            obj = t.getTransferData(ccDataFlavor);
        }
        catch (IOException ex) {
            dtc.dropComplete(false);
            return;
        }
        catch (UnsupportedFlavorException ex) {
            dtc.dropComplete(false);
            return;
        }
        if (obj!=null&&obj instanceof String) {
            theNewColor = parseColor((String)obj);
        }
        if (obj!=null&&obj instanceof InputStream) {
            InputStream input = (InputStream)obj;
            byte[] buffer = new byte[64];
            String color;
            try {
                int count = input.read(buffer);
                while (count!=-1) {
                    color = new String(buffer, 0, count);
                    theNewColor = parseColor(color);
                    count = input.read(buffer);
                }
            }
            catch (IOException e) {
                dtc.dropComplete(false);
                return;
            }
            try {
                input.close();
            }
            catch (IOException e) {
                dtc.dropComplete(false);
                return;
            }
        }
        if (theNewColor!=null) m_clrColor = theNewColor;
        repaint();
        dtc.dropComplete(true);
    }
    public void dropActionChanged(DropTargetDragEvent dtde) { ; }
}



4


