一、鼠标拖拽移动图片
package cn.util;import java.io.File;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;/**** @author MK*/
public class ImageApplication extends Application {Point2D dragDistance = null;public void start(Stage primaryStage) {ImageView imageView = new ImageView();String file = new File("picture1.png").toURI().toString();System.out.println(file);Image image = new Image(file);imageView.setImage(image);final double w = image.getWidth();final double h = image.getHeight();final double max = Math.max(w, h);final int width = (int) (500 * w / max);final int heigth = (int) (500 * h / max);imageView.setFitHeight(heigth);imageView.setFitWidth(width);Pane pane = new Pane();StackPane stackPane = new StackPane(pane);Scene scene = new Scene(stackPane, 700, 800);pane.getChildren().add(imageView);pane.heightProperty().addListener(new ChangeListener<Number>() {@Overridepublic void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {imageView.setY((newValue.doubleValue() - imageView.getFitHeight()) / 2);}});pane.widthProperty().addListener(new ChangeListener<Number>() {@Overridepublic void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {imageView.setX((newValue.doubleValue() - imageView.getFitWidth()) / 2);}});stackPane.addEventFilter(MouseDragEvent.MOUSE_PRESSED, event -> {dragDistance = new Point2D(event.getSceneX(), event.getSceneY());dragDistance = dragDistance.subtract(pane.localToScene(new Point2D(imageView.getX(), imageView.getY())));});stackPane.addEventFilter(MouseDragEvent.MOUSE_DRAGGED, event -> {if (event.isPrimaryButtonDown()) {Point2D px = new Point2D(event.getSceneX(), event.getSceneY());px = pane.sceneToLocal(px.subtract(dragDistance));imageView.setX(px.getX());imageView.setY(px.getY());}});primaryStage.setScene(scene);primaryStage.show();}public static void main(String[] args) {launch(args);}}