我正在尝试使用鼠标在图像之间画一条线但是当我拖动时,该线也没有被正确拖动。
public class line1 extends Application {
final Image image1=new Image("image/MME.png");
final Image image2=new Image("image/HSS.png");
final ImageView i1=new ImageView();
final ImageView i2=new ImageView();
Line line;
@Override
public void start(Stage primaryStage) throws Exception {
final BorderPane root = new BorderPane();
i1.setImage(image1);
i1.setLayoutX(200);
i1.setLayoutY(300);
i2.setImage(image2);
i2.setLayoutX(50);
i2.setLayoutY(50);
i1.setCursor(Cursor.CROSSHAIR);
root.getChildren().addAll(i1,i2);
root.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
double x = event.getX();
double y = event.getY();
line = new Line(x, y, x, y);
root.getChildren().add(line);
}
});
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
line.setEndX(event.getX());
line.setEndY(event.getY());
}
});
i1.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
line.setStartX(event.getScreenX());
line.setStartY(event.getScreenY());
line.setEndX(i2.getLayoutX());
line.setEndY(i2.getLayoutY());
i1.setLayoutX(event.getScreenX());
i1.setLayoutY(event.getScreenY());
root.getChildren().add(line);
}
});
primaryStage.setScene(new Scene(root, 1350, 850));
primaryStage.show();
}
public static void main(String[] args) {launch(args);}
}