below is my code for drag and drop and image for er diagram maker in java fx.. but image is dragging only once.. how to make it drag and drop multiple tyms and also how to save the dragged image... plz help me our as soon as possible
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ads; import java.io.File; import java.net.URL; import java.util.ResourceBundle; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Cursor; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.scene.input.*; import javafx.scene.layout.AnchorPane; import javafx.scene.text.Text; import javax.imageio.ImageIO; public class SampleController1 implements Initializable { // @FXML // public Button all[]=new Button[100]; @FXML public Button entity; @FXML public Button attribute; @FXML public ScrollPane draw; @FXML public AnchorPane test; Group root = new Group(//<editor-fold defaultstate="collapsed" desc="comment"> //</editor-fold> ); ImageView[] img={new ImageView("ads/icons/ent.png"),new ImageView("ads/icons/att.png"),new ImageView("ads/icons/rel.png")}; ImageView[] imgtest=new ImageView[50]; boolean flag=false; private Node[] children; //boolean test=false; @FXML public void handcursor(MouseEvent event) { entity.setCursor(Cursor.HAND); } /** * * @param event */ @FXML public void handleButtonAction(ActionEvent event) { System.out.println("You clicked me!dfgfdgdf"); } @FXML public void OnDragDetected(MouseEvent event) { System.out.println("onDragDetected"); Dragboard db = entity.startDragAndDrop(TransferMode.ANY); ClipboardContent content = new ClipboardContent(); content.putImage(img[0].getImage()); db.setContent(content); event.consume(); } @FXML public void OnDragDetectedPane(MouseEvent event) { System.out.println("onDragDetected"); Dragboard db = entity.startDragAndDrop(TransferMode.ANY); db.getTransferModes(); ClipboardContent content = new ClipboardContent(); content.putImage(img[0].getImage()); db.setContent(content); event.consume(); } @FXML public void OnDragDone(DragEvent event) { System.out.println("onDragDone"); if (event.getTransferMode() == TransferMode.MOVE) { System.out.println("hello"); } event.consume(); } @FXML public void OnDragDonePane(DragEvent event) { System.out.println("onDragDone"); if (event.getTransferMode() == TransferMode.MOVE) { System.out.println("hello"); } event.consume(); } @FXML public void OnDragDropped(DragEvent event) { System.out.println("onDragDropped"); boolean success = false; Dragboard db = event.getDragboard(); ClipboardContent content = new ClipboardContent(); if (flag==true) { //img.setFitHeight(event.getSceneY()); //img.setFitWidth(event.getSceneX()); success=true; flag=false; } event.setDropCompleted(success); event.consume(); } @FXML public void OnDragDroppedPane(DragEvent event) { System.out.println("onDragDropped"); boolean success = false; Dragboard db = event.getDragboard(); ClipboardContent content = new ClipboardContent(); if (flag==true) { //img.setFitHeight(event.getSceneY()); //img.setFitWidth(event.getSceneX()); success=true; flag=false; } event.setDropCompleted(success); event.consume(); } @FXML public Button temp; @FXML public Text t=new Text("Hello"); @FXML public void OnDragOver(DragEvent event) { System.out.println("onDragOver"); if (event.getGestureSource() != draw && event.getDragboard().hasImage()) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } Dragboard db = event.getDragboard(); //boolean success = false; if (db.hasImage()) { System.out.println("M in"); img[0].setLayoutX(event.getX()); img[0].setLayoutY(event.getY()); img[0].setImage(db.getImage()); draw.setContent(img[0]); flag=true; //success = true; } //event.setDropCompleted(success); event.consume(); } public void OnDragOverPane(DragEvent event) { System.out.println("onDragOver"); if (event.getGestureSource() != draw && event.getDragboard().hasImage()) { event.acceptTransferModes(TransferMode.COPY_OR_MOVE); } Dragboard db = event.getDragboard(); //boolean success = false; if (db.hasImage()) { System.out.println("M in"); img[0].setLayoutX(event.getX()); img[0].setLayoutY(event.getY()); img[0].setImage(db.getImage()); draw.setContent(img[0]); flag=true; //success = true; } WritableImage wim = new WritableImage(300, 250); img[1].snapshot(null, wim); File file = new File("ImageWithEffect.png"); try { ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file); } catch (Exception s) { } //event.setDropCompleted(success); event.consume(); } @Override public void initialize(URL url, ResourceBundle rb) { // TODO } }