c.f. 잘라내서-붙여넣기
Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException;
import java.awt.Color;
import java.awt.datatransfer.*;
import java.io.*;
public class TransferableColor implements Transferable {
protected static DataFlavor colorFlavor = new DataFlavor(Color.class, "Java Color Object");
protected static DataFlavor[] supportedFlavors = {
colorFlavor,
DataFlavor.stringFlavor,
DataFlavor.plainTextFlavor,
};
Color color;
public TransferableColor(Color color) { this.color = color; }
public DataFlavor[] getTransferDataFlavors() { return supportedFlavors; }
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor.equals(colorFlavor) ||
flavor.equals(DataFlavor.stringFlavor) ||
flavor.equals(DataFlavor.plainTextFlavor)) return true;
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if (flavor.equals(colorFlavor))
return color;
else if (flavor.equals(DataFlavor.stringFlavor))
return color.toString();
else if (flavor.equals(DataFlavor.plainTextFlavor))
return new ByteArrayInputStream(color.toString().getBytes("Unicode"));
else
throw new UnsupportedFlavorException(flavor);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
public class ColorSource extends JComponent implements ClipboardOwner, DragGestureListener, DragSourceListener
{
Color color;
TransferableColor tcolor;
DragSource dragSource;
protected static Border defaultBorder = new BevelBorder(BevelBorder.LOWERED);
protected static Border highlightBorder = new CompoundBorder(defaultBorder, new LineBorder(Color.black, 2));
public ColorSource(Color color) {
this.color = color;
this.tcolor = new TransferableColor(color);
this.setBorder(defaultBorder);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) { copy(); }
});
this.dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public void copy() {
Clipboard c = this.getToolkit().getSystemClipboard();
c.setContents(tcolor, this);
this.setBorder(highlightBorder);
}
protected static Dimension mysize = new Dimension(25, 25);
public Dimension getMinimumSize() { return mysize; }
public Dimension getPreferredSize() { return mysize; }
public void paintComponent(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);
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
this.setBorder(defaultBorder);
}
public void dragGestureRecognized(DragGestureEvent e) {
Image colorblock = this.createImage(25,25);
Graphics g = colorblock.getGraphics();
g.setColor(color);
g.fillRect(0,0,25,25);
e.startDrag(DragSource.DefaultMoveDrop, colorblock, new Point(0,0), tcolor, this);
}
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) {}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.List;
public class ColorSink extends JTextArea implements DropTargetListener {
public ColorSink() {
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
pastecolor();
e.consume();
}
}
});
setDropTarget(new DropTarget(this, this));
}
public void pastecolor() {
Clipboard c = this.getToolkit().getSystemClipboard();
Transferable t = c.getContents(this);
if (t == null) {
this.getToolkit().beep();
return;
}
try {
if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
Color color = (Color) t.getTransferData(TransferableColor.colorFlavor);
this.setBackground(color);
} else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
this.replaceSelection(s);
} else {
this.getToolkit().beep();
}
}
catch (UnsupportedFlavorException ex) { this.getToolkit().beep(); }
catch (IOException ex) { this.getToolkit().beep(); }
}
protected static Border dropBorder = new BevelBorder(BevelBorder.LOWERED);
public void dragEnter(DropTargetDragEvent e) {
if (e.isDataFlavorSupported(TransferableColor.colorFlavor) || e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
this.setBorder(dropBorder);
}
}
public void dragExit(DropTargetEvent e) { this.setBorder(null); }
public void drop(DropTargetDropEvent e){
this.setBorder(null);
Transferable t = e.getTransferable();
if (t.isDataFlavorSupported(TransferableColor.colorFlavor)) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try {
Color c = (Color) t.getTransferData(TransferableColor.colorFlavor);
this.setBackground(c);
e.dropComplete(true);
}
catch (Exception ex) { e.dropComplete(false); }
} else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
try {
List files = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
File f = (File) files.get(0);
BufferedReader in = new BufferedReader(new FileReader(f));
String s;
this.setText("");
while((s = in.readLine()) != null) this.append(s);
e.dropComplete(true);
}
catch (Exception ex) { e.dropComplete(false); }
} else {
e.rejectDrop();
return;
}
}
public void dragOver(DropTargetDragEvent e) {}
public void dropActionChanged(DropTargetDragEvent e) {}
public static void main(String[] args) {
JFrame f = new JFrame("..");
f.getContentPane().setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(new ColorSource(Color.yellow));
panel.add(new ColorSource(Color.pink));
..
ColorSink sink = new ColorSink();
f.getContentPane().add(panel, BorderLayout.NORTH);
f.getContentPane().add(sink, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
f.setVisible(true); // f.show();
}
}