Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package d;
- import java.util.List;
- import javafx.application.Application;
- import javafx.event.EventHandler;
- import javafx.scene.Cursor;
- import javafx.scene.Group;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.*;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- /**
- * Demonstrates a drag-and-drop feature.
- */
- public class HelloDragAndDrop extends Application {
- @Override public void start(Stage stage) {
- stage.setTitle("Hello Drag And Drop");
- Group root = new Group();
- Scene scene = new Scene(root, 800, 600);
- scene.setFill(Color.LIGHTGREEN);
- final Image source = new Image("./d/cache.jpg");
- ImageView carte = new ImageView(source);
- final Delta dr = new Delta();
- final Rectangle r = new Rectangle(400, 200,250, 400);
- r.setFill(Color.RED);
- //carte.setScaleX(2.0);
- //carte.setScaleY(2.0);
- carte.setOnMousePressed(new EventHandler<MouseEvent>() {
- @Override public void handle(MouseEvent mouseEvent) {
- // record a delta distance for the drag and drop operation.
- dr.x = carte.getLayoutX() - mouseEvent.getSceneX();
- dr.y = carte.getLayoutY() - mouseEvent.getSceneY();
- carte.setCursor(Cursor.MOVE);
- }
- });
- root.getChildren().add(r);
- root.getChildren().add(carte);
- stage.setScene(scene);
- stage.show();
- carte.setOnMouseReleased(new EventHandler<MouseEvent>() {
- @Override public void handle(MouseEvent mouseEvent) {
- System.out.println(carte.getLayoutX() + ";" + carte.getLayoutY());
- System.out.println(r);
- if((mouseEvent.getSceneX() >= r.getLayoutX()) && (mouseEvent.getSceneX()<= (r.getLayoutX()+r.getWidth())) && (mouseEvent.getSceneY() >= r.getLayoutY()) && (mouseEvent.getSceneY() <= (r.getLayoutY()+r.getHeight())) ){
- System.out.println("!!!!!?");
- }
- }
- });
- carte.setOnMouseDragged(new EventHandler<MouseEvent>() {
- @Override public void handle(MouseEvent mouseEvent) {
- carte.setLayoutX(mouseEvent.getSceneX() + dr.x);
- carte.setLayoutY(mouseEvent.getSceneY() + dr.y);
- }
- });
- }
- // records relative x and y co-ordinates.
- class Delta { double x, y; }
- public static void main(String[] args) {
- Application.launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement