我试图绘制一条垂直线和一条水平线,以指向鼠标所指向的位置,我希望看到“光标”随鼠标移动。
因此,我试图在图表中添加一条线,但由于线图没有getChildren()函数,因此无法添加任何内容。因此,我想要这样的东西:
我也在考虑这些“光标”是否可以成为对象,当我将类称为“光标”时,它将创建2条线,一条垂直线,一条水平线?
我的代码:
我尝试使用LineBuilder绘制一条线,但是现在我想要的是。 现在,我成功获得了鼠标的位置,现在如何绘制2条线?
NumberAxis x = new NumberAxis();
NumberAxis y = new NumberAxis();
LineChart<Number,Number> chart = new LineChart<Number,Number>(x,y);
XYChart.Series series1 = ...;
// creating a series then giving this series 100 random values
chart.getData().add(series1);
chartPane.getChildren().add(chart);
chart.setOnMousePressed((MouseEvent event) -> {
Point2D mouseSceneCoords = new Point2D(event.getSceneX(), event.getSceneY());
double x = xAxis.sceneToLocal(mouseSceneCoords).getX();
double y = yAxis.sceneToLocal(mouseSceneCoords).getY();
primaryStage.setTitle("" +
xAxis.getValueForDisplay(x) + ", " +
yAxis.getValueForDisplay(y)
);
});
因此,既然我现在可以成功将您的代码应用到我的应用程序上,那么我就有一个问题: 我的鼠标指向该点(800,10),但是光标完全远离我的鼠标的位置。
答案 0 :(得分:5)
密钥正在使用StackPane
。这样,您可以添加一个Pane
来处理这两行。这是一个粗略的示例:
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ValueAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class LineChartExperiments extends Application
{
double mouseX = 0;
double mouseY = 0;
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("LineChart Experiments");
ValueAxis xAxis = new NumberAxis();
xAxis.setLabel("No of employees");
ValueAxis yAxis = new NumberAxis();
yAxis.setLabel("Revenue per employee");
LineChart lineChart = new LineChart(xAxis, yAxis);
XYChart.Series dataSeries1 = new XYChart.Series();
dataSeries1.setName("2014");
dataSeries1.getData().add(new XYChart.Data(1, 567));
dataSeries1.getData().add(new XYChart.Data(5, 612));
dataSeries1.getData().add(new XYChart.Data(10, 800));
dataSeries1.getData().add(new XYChart.Data(20, 780));
dataSeries1.getData().add(new XYChart.Data(40, 810));
dataSeries1.getData().add(new XYChart.Data(80, 850));
lineChart.getData().add(dataSeries1);
Line verticleLine = new Line();
verticleLine.setStrokeWidth(3);
Line horizontalLine = new Line();
horizontalLine.setStrokeWidth(3);
Pane pane = new Pane(verticleLine, horizontalLine);
AnimationTimer loop = new AnimationTimer()
{
@Override
public void handle(long now)
{
verticleLine.setStartY(0);
verticleLine.setEndY(pane.getHeight());
verticleLine.setEndX(mouseX);
verticleLine.setStartX(mouseX);
horizontalLine.setStartX(0);
horizontalLine.setEndX(pane.getWidth());
horizontalLine.setEndY(mouseY);
horizontalLine.setStartY(mouseY);
}
};
pane.addEventFilter(MouseEvent.ANY, event -> {
mouseX = event.getSceneX();
mouseY = event.getSceneY();
loop.start();
});
StackPane stackPane = new StackPane(lineChart, pane);
Scene scene = new Scene(stackPane, 400, 200);
primaryStage.setScene(scene);
primaryStage.setHeight(500);
primaryStage.setWidth(700);
primaryStage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
}
**此代码有很多缺陷,仅应用作概念证明。