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

[14905 byte] By [sunnymanmana] at [2007-11-26 19:57:46]
# 1
[url http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html#dataFormat]Using Drag and Drop[/url] to specify the data that is to be dragged and dropped.
camickra at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks, but still very confused. I am newbie of swing,here I hope to drop icon/image from another panel to this empty panel, but here all panel have labels already.any further advice
sunnymanmana at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...
# 3
I have a panel with a lot of smaple icon and images alraedy, I need to build another empty panel to receive or drop these image/icon dynamically at run-time, need help.thanks
sunnymanmana at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...
# 4
> I need to build another empty panel to receive or drop these image/icon Are you creating a copy of the icon and dropping it on the panel, or are you moving the actual icon from one panel to another?
camickra at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...
# 5
I am creating a copy of the icon and dropping it on the panel, not actual oneThanks
sunnymanmana at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...
# 6

Well, I don't really understand the DnD interface so maybe my chess board example will be easier to understand and modify:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707

Basically what you would need to do is:

a) on a mousePressed you would need to create a new JLabel with the icon from the clicked label and add the label to the glass pane

b) mouseDragged code would be the same you just repaint the label as it is moved

c) on a mouseReleased, you would need to check that the label is now positioned over your "drop panel" and then add the label to the panel using the panels coordinates not the glass pane coordinates.

camickra at 2007-7-9 22:52:53 > top of Java-index,Desktop,Core GUI APIs...