简体   繁体   English

Scollpane 中的 JavaFx 拖动形状

[英]JavaFx drag Shape in Scollpane

I am currently creating a puzzle game.我目前正在创建一个益智游戏。 The idea is that on the left you have a ScrollPane containing a pane which has all the puzzle pieces (javafx.shapes).这个想法是,在左边你有一个 ScrollPane,其中包含一个包含所有拼图 (javafx.shapes) 的窗格。

I have code that allows me to drag around the pieces but if I want to drag it to the bottom of the ScrollPane it flies back up to its initial position.我有允许我在各个部分周围拖动的代码,但是如果我想将它拖动到 ScrollPane 的底部,它会飞回其初始位置。 I want the shapes to be draggable from the top of the ScrollPane to the Bottom.我希望形状可以从 ScrollPane 的顶部拖动到底部。 在此处输入图片说明

Controller:控制器:

public class SpielModus extends Application{
    @FXML
    private ScrollPane pieceAreaScrollable;

    @FXML
    private Pane pieceArea;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.stage = primaryStage;
        show();
    }

    @FXML
    public void initialize() {
        Circle circle = new Circle(50, 50, 50);
        Circle circle2 = new Circle(50, 200, 50);

        Group group = new Group(circle, circle2);

        // So that the Pane is bigger then ScrollPane to become Scrollable
        pieceArea.setMinHeight(2000);
        pieceArea.getChildren().addAll(group);

        group.setOnMouseDragged(e -> {
            group.setLayoutX(e.getSceneX());
            group.setLayoutY(e.getSceneY());
        });
    }

    public void show() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("SpielModus.fxml"));
            loader.setController(this);

            Parent layout = loader.load();
            stage.setScene(new Scene(layout));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FXML:文件格式:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1080.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171">
  <columnConstraints>
    <ColumnConstraints minWidth="10.0" prefWidth="250.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <ScrollPane fx:id="pieceAreaScrollable" hbarPolicy="NEVER" prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.rowSpan="2">
         <content>
            <Pane fx:id="pieceArea" prefHeight="200.0" prefWidth="200.0" />
         </content>
      </ScrollPane>
   </children>
</GridPane>

You're relocating the circles within the large pane pieceArea to a location based on the mouse coordinates relative to the scene.你将迁往大窗格中的圆圈pieceArea基于鼠标的位置坐标相对于场景。 If the pieceArea has been scrolled, this will give the incorrect location (because it doesn't account for the scrolling).如果已经滚动了pieceArea ,这将给出不正确的位置(因为它不考虑滚动)。 You need the mouse coordinates relative to pieceArea (the parent of the group , which is the node on which the mouse drag event occurs).您需要相对于pieceAreagroup的父级,即发生鼠标拖动事件的节点)的鼠标坐标。

You can do this with你可以这样做

    group.setOnMouseDragged(e -> {
        Point2D newLocation = group.localToParent(new Point2D(e.getX(), e.getY()));
        group.setLayoutX(newLocation.getX());
        group.setLayoutY(newLocation.getY());
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM