How to change this code to drag and drop image/icon into it?
Dear Friends:
I have following code is for drag/drop label, But I need to change to drag and drop image/icon into it.
[1]. code 1:
package swing.dnd;
import java.awt.*;
import javax.swing.*;
publicclass TestDragComponentextends JFrame{
public TestDragComponent(){
super("Test");
Container c = getContentPane();
c.setLayout(new GridLayout(1,2));
c.add(new MoveableComponentsContainer());
c.add(new MoveableComponentsContainer());
pack();
setVisible(true);
}
publicstaticvoid main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex){
System.out.println(ex);
}
new TestDragComponent();
}
}
[2]. Code 2:
package swing.dnd;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
publicclass MoveableComponentsContainerextends JPanel{
public DragSource dragSource;
public DropTarget dropTarget;
privatestatic BufferedImage buffImage =null;//buff image
privatestatic Point cursorPoint =new Point();
public MoveableComponentsContainer(){
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
setLayout(null);
dragSource =new DragSource();
ComponentDragSourceListener tdsl =new ComponentDragSourceListener();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE,new ComponentDragGestureListener(tdsl));
ComponentDropTargetListener tdtl =new ComponentDropTargetListener();
dropTarget =new DropTarget(this, DnDConstants.ACTION_MOVE, tdtl);
setPreferredSize(new Dimension(400,400));
addMoveableComponents();
}
privatevoid addMoveableComponents(){
MoveableLabel lab =new MoveableLabel("label 1");
add(lab);
lab.setLocation(10,10);
lab =new MoveableLabel("label 2");
add(lab);
lab.setLocation(40,40);
lab =new MoveableLabel("label 3");
add(lab);
lab.setLocation(70,70);
lab =new MoveableLabel("label 4");
add(lab);
lab.setLocation(100,100);
}
finalclass ComponentDragSourceListenerimplements DragSourceListener{
publicvoid dragDropEnd(DragSourceDropEvent dsde){
}
publicvoid dragEnter(DragSourceDragEvent dsde){
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_MOVE){
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
}
else{
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
}
}
publicvoid dragOver(DragSourceDragEvent dsde){
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_MOVE){
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
}
else{
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
}
}
publicvoid dropActionChanged(DragSourceDragEvent dsde){
int action = dsde.getDropAction();
if (action == DnDConstants.ACTION_MOVE){
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
}
else{
dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
}
}
publicvoid dragExit(DragSourceEvent dse){
dse.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
}
}
finalclass ComponentDragGestureListenerimplements DragGestureListener{
ComponentDragSourceListener tdsl;
public ComponentDragGestureListener(ComponentDragSourceListener tdsl){
this.tdsl = tdsl;
}
publicvoid dragGestureRecognized(DragGestureEvent dge){
Component comp = getComponentAt(dge.getDragOrigin());
if (comp !=null && comp != MoveableComponentsContainer.this){
cursorPoint.setLocation(SwingUtilities.convertPoint(MoveableComponentsContainer.this, dge.getDragOrigin(), comp));
buffImage =new BufferedImage(comp.getWidth(), comp.getHeight(), java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);//buffered image reference passing the label's ht and width
Graphics2D graphics = buffImage.createGraphics();//creating the graphics for buffered image
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));//Sets the Composite for the Graphics2D context
boolean opacity = ((JComponent)comp).isOpaque();
if (opacity){
((JComponent)comp).setOpaque(false);
}
comp.paint(graphics);//painting the graphics to label
if (opacity){
((JComponent)comp).setOpaque(true);
}
graphics.dispose();
remove(comp);
dragSource.startDrag(dge, DragSource.DefaultMoveDrop , buffImage, cursorPoint,new TransferableComponent(comp), tdsl);
revalidate();
repaint();
}
}
}
finalclass ComponentDropTargetListenerimplements DropTargetListener{
private Rectangle rect2D =new Rectangle();
Insets insets;
publicvoid dragEnter(DropTargetDragEvent dtde){
Point pt = dtde.getLocation();
paintImmediately(rect2D.getBounds());
rect2D.setRect((int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),buffImage.getWidth(),buffImage.getHeight());
((Graphics2D) getGraphics()).drawImage(buffImage,(int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),MoveableComponentsContainer.this);
dtde.acceptDrag(dtde.getDropAction());
}
publicvoid dragExit(DropTargetEvent dte){
paintImmediately(rect2D.getBounds());
}
publicvoid dragOver(DropTargetDragEvent dtde){
Point pt = dtde.getLocation();
paintImmediately(rect2D.getBounds());
rect2D.setRect((int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),buffImage.getWidth(),buffImage.getHeight());
((Graphics2D) getGraphics()).drawImage(buffImage,(int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),MoveableComponentsContainer.this);
dtde.acceptDrag(dtde.getDropAction());
}
publicvoid dropActionChanged(DropTargetDragEvent dtde){
Point pt = dtde.getLocation();
paintImmediately(rect2D.getBounds());
rect2D.setRect((int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),buffImage.getWidth(),buffImage.getHeight());
((Graphics2D) getGraphics()).drawImage(buffImage,(int) (pt.getX()-cursorPoint.getX()),(int) (pt.getY()-cursorPoint.getY()),MoveableComponentsContainer.this);
dtde.acceptDrag(dtde.getDropAction());
}
publicvoid drop(DropTargetDropEvent dtde){
try{
paintImmediately(rect2D.getBounds());
int action = dtde.getDropAction();
Transferable transferable = dtde.getTransferable();
if (transferable.isDataFlavorSupported(TransferableComponent.COMPONENT_FLAVOR)){
Component comp = (Component) transferable.getTransferData(TransferableComponent.COMPONENT_FLAVOR);
Point location = dtde.getLocation();
if (comp ==null){
dtde.rejectDrop();
dtde.dropComplete(false);
revalidate();
repaint();
return;
}
else{
add(comp, 0);
comp.setLocation((int)(location.getX()-cursorPoint.getX()),(int)(location.getY()-cursorPoint.getY()));
dtde.dropComplete(true);
revalidate();
repaint();
return;
}
}
else{
dtde.rejectDrop();
dtde.dropComplete(false);
return;
}
}
catch (Exception e){
System.out.println(e);
dtde.rejectDrop();
dtde.dropComplete(false);
}
}
}
}
Thanks so much for any help.
Reagrds
sunny

