gpt4 book ai didi

java - 是否可以将 javaFX Pane 原点设置为左下角?

转载 作者:行者123 更新时间:2023-11-30 08:51:07 24 4
gpt4 key购买 nike

是否可以将 Pane 的原点设置为左下角(而不是左上角)?我在我的 Canvas 上添加了许多形状,这些形状是在“数学坐标系”中定义的。

我想也许有一种比总是从 y 坐标中减去高度更简单的方法。另一个优势是我不必负责调整 Pane 的大小。

最佳答案

如果您所做的只是使用形状,则可以对 Pane 应用反射。您可以将反射表示为 Scale x=1y=-1。唯一棘手的部分是您必须将比例尺的轴心保持在垂直中心,这需要绑定(bind)以防 Pane 大小发生变化。

如果您在 Pane 中放置控件或文本,那么它们也会被反射(reflect)出来,因此它们看起来不正确。但如果您所做的只是使用形状,这将有效。

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ReflectedPaneTest extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Scale scale = new Scale();
scale.setX(1);
scale.setY(-1);

scale.pivotYProperty().bind(Bindings.createDoubleBinding(() ->
pane.getBoundsInLocal().getMinY() + pane.getBoundsInLocal().getHeight() /2,
pane.boundsInLocalProperty()));
pane.getTransforms().add(scale);

pane.setOnMouseClicked(e ->
System.out.printf("Mouse clicked at [%.1f, %.1f]%n", e.getX(), e.getY()));

primaryStage.setScene(new Scene(pane, 600, 400));
primaryStage.show();
}

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

关于java - 是否可以将 javaFX Pane 原点设置为左下角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30695887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com