JavaFX |更换线锚?

时间:2019-03-16 12:39:57

标签: javafx

我尝试绘制一条线并在拖动时更改其结束坐标。 问题是,它改变了两条线的两端,相对于中间线(可能是锚点)。

是否可以将锚点移动到行首?

我的代码是:

        Line path = new Line(30,30, 70 , 75);
        path.setStrokeWidth(5);

        Circle point = new Circle(3);
        point.setCenterX(path.getEndX());
        point.setCenterY(path.getEndY());
        point.setFill(Paint.valueOf("red"));

        point.setOnMouseDragged(e ->{

            point.setCenterX(e.getX());
            point.setCenterY(e.getY());

            path.setEndX(point.getCenterX());
            path.setEndY(point.getCenterY());

        });

        Group shapes = new Group();
        shapes.getChildren().addAll(path, point);
        BorderPane root = new BorderPane(shapes);
        Scene scene = new Scene(root,600,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());


        primaryStage.setScene(scene);



        primaryStage.show();

结果:

enter image description here
理想的情况是,枢轴点将位于直线的起点,而不是中间。

2 个答案:

答案 0 :(得分:0)

按组放置形状。

public void start(final Stage primaryStage) throws AWTException {
    final Line path = new Line(30, 30, 70, 75);
    path.setStrokeWidth(5);

    final Circle point = new Circle(3);
    point.setCenterX(path.getEndX());
    point.setCenterY(path.getEndY());
    point.setFill(Paint.valueOf("red"));

    point.setOnMouseDragged(e -> {

        point.setCenterX(e.getX());
        point.setCenterY(e.getY());

        path.setEndX(point.getCenterX());
        path.setEndY(point.getCenterY());

    });

    final Group root = new Group(path, point);
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

答案 1 :(得分:0)

您所看到的并不是线在两个方向上都在增长或缩小。取而代之的是,随着线长度的变化,BorderPane将其重新定位以使其居中。 “旋转”发生了相同的事情。当您以改变线角度的方式移动一端时,边界框的更改方式是当BorderPane重新放置线时,会导致另一端朝相反的方向移动。 / p>

更具体地说,BorderPane正在重新定位Group,这实际上是同一件事,因为Group并未放置其子级。一种解决方法是使Group不受管理。随着边界的变化,这将阻止BorderPane重新定位Group。但是请注意,这也会阻止Group促进BorderPane的大小和布局计算。

这是一个例子:

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

    private static void installDragHandlers(Circle circle) {
        circle.setOnMousePressed(event -> {
            Point2D offset = new Point2D(
                    event.getX() - circle.getCenterX(),
                    event.getY() - circle.getCenterY()
            );
            circle.setUserData(offset);
            event.consume();
        });
        circle.setOnMouseDragged(event -> {
            Point2D offset = (Point2D) circle.getUserData();
            circle.setCenterX(event.getX() - offset.getX());
            circle.setCenterY(event.getY() - offset.getY());
            event.consume();
        });
        circle.setOnMouseReleased(event -> {
            circle.setUserData(null);
            event.consume();
        });
    }

    @Override
    public void start(Stage primaryStage) {
        Line line = new Line(200, 200, 400, 200);
        line.setStrokeWidth(2);

        Circle start = new Circle(5, Color.GREEN);
        start.centerXProperty().bindBidirectional(line.startXProperty());
        start.centerYProperty().bindBidirectional(line.startYProperty());
        installDragHandlers(start);

        Circle end = new Circle(5, Color.RED);
        end.centerXProperty().bindBidirectional(line.endXProperty());
        end.centerYProperty().bindBidirectional(line.endYProperty());
        installDragHandlers(end);

        Group group = new Group(line, start, end);
        group.setManaged(false);

        primaryStage.setScene(new Scene(new BorderPane(group), 600, 400));
        primaryStage.setTitle("SO-55196882");
        primaryStage.show();
    }

}

Line使用硬编码的起点和终点进行初始化,以使其最初位于场景的中心(其初始尺寸也采用硬编码)。