【问题标题】:is it possible to set a javaFX Pane origin to bottom-left?是否可以将 javaFX 窗格原点设置为左下角?
【发布时间】:2015-06-07 16:31:18
【问题描述】:

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

我认为也许有比总是从 y 坐标中减去高度更简单的方法。另一个优点是我不必调整窗格大小。

【问题讨论】:

    标签: java javafx


    【解决方案1】:

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

    如果您在窗格中放置控件或文本,那么它们也会被反映,因此它们看起来不正确。但是,如果您所做的只是使用形状,这将起作用。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多