无法在Javafx中旋转形状

时间:2019-07-03 00:49:58

标签: javafx

我无法在JavaFX中旋转形状。这是一些代码,我希望箭头指向与线相同的方向(是的,我稍后将添加翻译,以将其放在行的末尾,但一次只做一件事)。

println语句向我显示theta在正确地移动起点和终点时正在正确更新,但是箭头却顽固地保持在原始角度。为了使形状旋转,我还需要执行另一步骤吗?还是应该以其他方式进行?

package application.view;

public class Connector extends Group {
    private final DoubleProperty startX;
    private final DoubleProperty startY;
    private final DoubleProperty endX;
    private final DoubleProperty endY;

    public Connector(Line line, Polygon arrowhead, Preferences prefs) {
        arrowhead.setStroke(Color.BLACK);
        arrowhead.setStrokeWidth(1);
        arrowhead.setFill(Color.CORNFLOWERBLUE);
        startX = new SimpleDoubleProperty();
        startY = new SimpleDoubleProperty();
        endX = new SimpleDoubleProperty();
        endY = new SimpleDoubleProperty();
        thetaBinding = new DoubleBinding() {
            {
                super.bind(startX, startY, endX, endY);
            }
            @Override
            protected double computeValue() {
                return Math.atan2(getEndY() - getStartY(), getEndX() - getStartX());
            }
        };

        theta = new SimpleDoubleProperty();
        theta.bind(thetaBinding);

        theta.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable,
                    Number oldValue, Number newValue) {
                System.out.println(newValue);
                arrowhead.rotateProperty().set((Double)newValue);
            }
        });

        lineLength = new DoubleBinding() {
            {
                super.bind(startX, startY, endX, endY);
            }
            @Override
            protected double computeValue() {
                return Math.hypot(getEndX() - getStartX(), getEndY() - getStartY()) - prefs.getDouble("CONNECTOR_ARROW_LENGTH", Defaults.CONNECTOR_ARROW_LENGTH);
            }
        };

        lineEndX = new DoubleBinding() {
            {
                super.bind(startX, lineLength, theta);
            }
            @Override
            protected double computeValue() {
                return startX.get() + lineLength.get() * Math.cos(theta.get());
            }
        };

        lineEndY = new DoubleBinding() {
            {
                super.bind(startY, lineLength, theta);
            }
            @Override
            protected double computeValue() {
                return startY.get() + lineLength.get() * Math.sin(theta.get());
            }
        };

        arrowhead.getPoints().setAll(new Double[] {
                0.0, 0.0,
                0.0, prefs.getDouble("CONNECTOR_ARROW_HALF_WIDTH", Defaults.CONNECTOR_ARROW_HALF_WIDTH),
                prefs.getDouble("CONNECTOR_ARROW_LENGTH", Defaults.CONNECTOR_ARROW_LENGTH), 0.0,
                0.0, -prefs.getDouble("CONNECTOR_ARROW_HALF_WIDTH", Defaults.CONNECTOR_ARROW_HALF_WIDTH),
        });

        line.startXProperty().bind(startX);
        line.startYProperty().bind(startY);
        line.endXProperty().bind(lineEndX);
        line.endYProperty().bind(lineEndY);

        getChildren().addAll(line, arrowhead);
    }

    public final DoubleBinding thetaBinding;
    public final DoubleProperty theta;
    public final DoubleBinding lineLength;
    public final DoubleBinding lineEndX;
    public final DoubleBinding lineEndY;

    public double getStartX() {
        return startX.get();
    }

    public void setStartX(double value) {
        startX.set(value);
    }

    public DoubleProperty startXProperty() {
        return startX;
    }

    public double getStartY() {
        return startY.get();
    }

    public void setStartY(double value) {
        startY.set(value);
    }

    public DoubleProperty startYProperty() {
        return startY;
    }

    public double getEndX() {
        return endX.get();
    }

    public void setEndX(double value) {
        endX.set(value);
    }

    public DoubleProperty endXProperty() {
        return endX;
    }

    public double getEndY() {
        return endY.get();
    }

    public void setEndY(double value) {
        endY.set(value);
    }

    public DoubleProperty endYProperty() {
        return endY;
    }
    }

0 个答案:

没有答案