Advertisement
valentine2009

DRAG&DROP

Dec 9th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package d;
  2.  
  3. import java.util.List;
  4.  
  5. import javafx.application.Application;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Cursor;
  8. import javafx.scene.Group;
  9. import javafx.scene.Node;
  10. import javafx.scene.Scene;
  11. import javafx.scene.image.Image;
  12. import javafx.scene.image.ImageView;
  13. import javafx.scene.input.*;
  14. import javafx.scene.paint.Color;
  15. import javafx.scene.shape.Rectangle;
  16. import javafx.scene.text.Text;
  17. import javafx.stage.Stage;
  18.  
  19. /**
  20.  * Demonstrates a drag-and-drop feature.
  21.  */
  22. public class HelloDragAndDrop extends Application {
  23.  
  24.     @Override public void start(Stage stage) {
  25.         stage.setTitle("Hello Drag And Drop");
  26.  
  27.  
  28.         Group root = new Group();
  29.         Scene scene = new Scene(root, 800, 600);
  30.         scene.setFill(Color.LIGHTGREEN);
  31.  
  32.         final  Image source = new Image("./d/cache.jpg");
  33.         ImageView carte = new ImageView(source);
  34.        final Delta dr = new Delta();
  35.        final Rectangle r = new Rectangle(400, 200,250, 400);
  36.        r.setFill(Color.RED);
  37.         //carte.setScaleX(2.0);
  38.        //carte.setScaleY(2.0);
  39.  
  40.         carte.setOnMousePressed(new EventHandler<MouseEvent>() {
  41.           @Override public void handle(MouseEvent mouseEvent) {
  42.             // record a delta distance for the drag and drop operation.
  43.            dr.x = carte.getLayoutX() - mouseEvent.getSceneX();
  44.           dr.y = carte.getLayoutY() - mouseEvent.getSceneY();
  45.             carte.setCursor(Cursor.MOVE);
  46.           }
  47.         });
  48.  
  49.         root.getChildren().add(r);
  50.         root.getChildren().add(carte);
  51.  
  52.         stage.setScene(scene);
  53.         stage.show();
  54.  
  55.         carte.setOnMouseReleased(new EventHandler<MouseEvent>() {
  56.             @Override public void handle(MouseEvent mouseEvent) {
  57.                 System.out.println(carte.getLayoutX() + ";" + carte.getLayoutY());
  58.                 System.out.println(r);
  59.  
  60.               if((mouseEvent.getSceneX() >= r.getLayoutX()) && (mouseEvent.getSceneX()<= (r.getLayoutX()+r.getWidth())) && (mouseEvent.getSceneY() >= r.getLayoutY()) && (mouseEvent.getSceneY() <= (r.getLayoutY()+r.getHeight())) ){
  61.                     System.out.println("!!!!!?");
  62.               }
  63.  
  64.  
  65.  
  66.             }
  67.           });
  68.  
  69.           carte.setOnMouseDragged(new EventHandler<MouseEvent>() {
  70.             @Override public void handle(MouseEvent mouseEvent) {
  71.               carte.setLayoutX(mouseEvent.getSceneX() + dr.x);
  72.               carte.setLayoutY(mouseEvent.getSceneY() + dr.y);
  73.             }
  74.           });
  75.     }
  76.  // records relative x and y co-ordinates.
  77.     class Delta { double x, y; }
  78.  
  79.     public static void main(String[] args) {
  80.         Application.launch(args);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement