JavaFX:有没有一种方法可以使Y轴正向上,原点在左下角?
JavaFX坐标系从屏幕顶部绘制Y坐标,向下为正。我希望它是积极的向上,并从屏幕底部开始 需要翻译,文本节点需要翻转 有了它,我们希望绘制的矩形将以我们在数学课上“自然”的方式定位。其左下角位于原点,扩展到右上角JavaFX:有没有一种方法可以使Y轴正向上,原点在左下角?,java,javafx,Java,Javafx,JavaFX坐标系从屏幕顶部绘制Y坐标,向下为正。我希望它是积极的向上,并从屏幕底部开始 需要翻译,文本节点需要翻转 有了它,我们希望绘制的矩形将以我们在数学课上“自然”的方式定位。其左下角位于原点,扩展到右上角 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.chart.*; import javafx.scene.layout.VBox; import javafx.s
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);
}
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.chart.*;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
公共类FlippedExampleChart扩展了应用程序{
专用线图;
@凌驾
public void start(Stage primaryStage)引发异常{
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
//翻转轴
yAxis.setScaleY(-1);
//TODO如何翻译到屏幕底部。
//TODO如何翻转文本节点。
this.chart=新折线图(xAxis,yAxis){
@凌驾
public void layoutPlotChildren(){
super.layoutPlotChildren();
双倍高度=yAxis.getDisplayPosition(100);
矩形r=新矩形(0,0,50,高度);
r、 设置填充(颜色为绿色);
getPlotChildren().addAll(r);
}
};
this.chart.setAnimated(false);
VBox VBox=新的VBox(this.chart);
场景=新场景(vbox,400200);
初级阶段。场景(场景);
初生阶段。设定高度(600);
初生阶段:坐骨宽度(400);
primaryStage.show();
}
公共静态void main(字符串[]args){
应用程序启动(args);
}
}
我假设这里的目标是使用图表轴定义的坐标系绘制形状 最简单的方法可能是变换形状而不是轴。您可以为此创建实用程序方法:
private Transform chartDisplayTransform(NumberAxis xAxis, NumberAxis yAxis) {
return new Affine(
xAxis.getScale(), 0, xAxis.getDisplayPosition(0),
0, yAxis.getScale(), yAxis.getDisplayPosition(0)
);
}
关于代码的另一个注意事项是:layoutPlotChildren()
方法不一定会删除节点,因此最终添加的矩形可能比您发布的代码预期的要多
下面是使用此方法的代码版本(并确保矩形只添加一次)
另请参见这并不是您所要求的,而是将y轴比例上的y值转换为坐标系中的值。因此,不要对轴进行平移,只需在轴的坐标系中工作,并使用该方法变换变量即可。@James_D我熟悉该方法。我想更进一步,因为增加了心理数学开销。我想我假设您希望在与轴相同的坐标系中绘制矩形(即沿常规图表轴跨越
(0,0
)到(50100)
)。但也许你真的想在像素坐标系下工作(很难找到一个好的用例?难点在于使用矩形,因为需要的是宽度和高度,而不是顶点的坐标。这是和其他形状。有趣的方法。我需要看看它有多复杂,可以与多种形状。我希望有一种方法将其设置在轴上。@BAR您可以对所有形状应用相同的变换;应该很容易。您并不真的想变换轴,因为(如您所见),这意味着它们将显示在“显示空间”而不是逻辑“图表空间”中。似乎与ValueAxis::getDisplayPosition不兼容。更新的问题。哦,我明白了,矩形现在是图表坐标而不是屏幕坐标。但是现在ValueAxis::getDisplayPosition的倒数是什么。@BAR倒数函数是(它可以用于处理用户事件;例如,通过鼠标单击获取图表坐标)。
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);
}
}
@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();
}