gpt4 book ai didi

JavaFX:有没有办法让Y轴向上为正,原点在左下角?

转载 作者:行者123 更新时间:2023-12-02 16:16:42 25 4
gpt4 key购买 nike

JavaFX坐标系从屏幕顶部绘制Y坐标,向下为正值。我希望它向上为正,并从屏幕底部开始。

需要翻译,并且文本节点需要翻转。

这样,希望绘制的矩形能够以我们在数学课上所做的“自然”方式定位。以左下角为原点,向右上角扩展。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class FlippedExampleChart extends Application {

private LineChart<Number, Number> chart;

@Override
public void start(Stage primaryStage) throws Exception {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();

// Flip the axis
yAxis.setScaleY(-1);

// TODO How to translate to bottom of screen.
// TODO How to flip the text nodes.

this.chart = new LineChart<Number, Number>(xAxis, yAxis) {
@Override
public void layoutPlotChildren() {
super.layoutPlotChildren();

double height = yAxis.getDisplayPosition(100);

Rectangle r = new Rectangle(0, 0, 50, height);
r.setFill(Color.GREEN);
getPlotChildren().addAll(r);
}
};
this.chart.setAnimated(false);

VBox vbox = new VBox(this.chart);

Scene scene = new Scene(vbox, 400, 200);
primaryStage.setScene(scene);
primaryStage.setHeight(600);
primaryStage.setWidth(400);
primaryStage.show();
}

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

enter image description here

最佳答案

我假设这里的目标是使用图表轴定义的坐标系绘制形状。

最简单的方法可能是变换形状而不是轴。您可以为此创建一个实用方法:

private Transform chartDisplayTransform(NumberAxis xAxis, NumberAxis yAxis) {
return new Affine(
xAxis.getScale(), 0, xAxis.getDisplayPosition(0),
0, yAxis.getScale(), yAxis.getDisplayPosition(0)
);
}

关于您的代码的另一个注意事项:layoutPlotChildren() 方法不一定会删除节点,因此您最终可能会添加比您发布的代码预期更多的矩形。

这是使用此方法的代码版本(并确保矩形仅添加一次)。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Transform;
import javafx.stage.Stage;

public class FlippedExampleChart extends Application {

private LineChart<Number, Number> chart;

@Override
public void start(Stage primaryStage) throws Exception {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();

// Flip the axis
// yAxis.setScaleY(-1);

Rectangle r = new Rectangle(0, 0, 50, 100);
r.setFill(Color.GREEN);

this.chart = new LineChart<Number, Number>(xAxis, yAxis) {
@Override
public void layoutPlotChildren() {
super.layoutPlotChildren();
r.getTransforms().setAll(chartDisplayTransform(xAxis, yAxis));
// note nodes don't get removed from the plot children, and this method may be
// called often:
if (!getPlotChildren().contains(r)) {
getPlotChildren().add(r);
}
}
};
this.chart.setAnimated(false);

VBox vbox = new VBox(this.chart);

Scene scene = new Scene(vbox, 400, 200);
primaryStage.setScene(scene);
primaryStage.setHeight(600);
primaryStage.setWidth(400);
primaryStage.show();
}

private Transform chartDisplayTransform(NumberAxis xAxis, NumberAxis yAxis) {
return new Affine(xAxis.getScale(), 0, xAxis.getDisplayPosition(0), 0, yAxis.getScale(),
yAxis.getDisplayPosition(0));
}

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

结果:

enter image description here

如果您有多个节点要以这种方式处理,则策略是将它们添加到中,并将转换应用于:

@Override
public void start(Stage primaryStage) throws Exception {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();

Group extraNodes = new Group();

this.chart = new LineChart<Number, Number>(xAxis, yAxis) {
@Override
public void layoutPlotChildren() {
super.layoutPlotChildren();
Rectangle r1 = new Rectangle(0, 0, 50, 100);
r1.setFill(Color.GREEN);
Rectangle r2 = new Rectangle(70, 0, 30, 20);
r2.setFill(Color.AQUAMARINE);
extraNodes.getChildren().setAll(r1, r2);
extraNodes.getTransforms().setAll(chartDisplayTransform(xAxis, yAxis));
// note nodes don't get removed from the plot children, and this method may be
// called often:
if (!getPlotChildren().contains(extraNodes)) {
getPlotChildren().add(extraNodes);
}
}
};
this.chart.setAnimated(false);

VBox vbox = new VBox(this.chart);

Scene scene = new Scene(vbox, 400, 200);
primaryStage.setScene(scene);
primaryStage.setHeight(600);
primaryStage.setWidth(400);
primaryStage.show();
}

另请参阅this related question

关于JavaFX:有没有办法让Y轴向上为正,原点在左下角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60269596/

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