启动应用两次或更多次

时间:2019-07-22 21:21:07

标签: java javafx-8

我想创建一个JavaFx图表工厂。在这个(How to get an image file from a Javafx chart(linechart) without actually rendering it?)线程中,我发布了一个有关在FX应用程序窗口上生成实际渲染图的问题。 但是,现在我想多次实现该解决方案,所以我得到了“线程“主”中的异常” java.lang.IllegalStateException:应用程序启动不得被多次调用。

我使用两个类。 StartChartsFx类具有主要的非FX线程,从这里我创建数据并触发图表创建过程。 ChartFactory类启动FX线程并生成png图表。

这是StartChartsFx类:

public class StartChartsFx {

    public static void main(String[] args) {

        // It can be run just once ChartFactory chf=new ChartFactory();
        HashMap<Number, Number> xyData=new HashMap<>();
        xyData.put(1, 23);
        xyData.put(2, 14);
        xyData.put(3, 15);
        xyData.put(4, 24);
        xyData.put(5, 34);
        xyData.put(6, 36);
        xyData.put(7, 22);
        xyData.put(8, 45);
        xyData.put(9, 43);
        xyData.put(10, 17);
        xyData.put(11, 29);
        xyData.put(12, 25);

        ChartFactory chf=new ChartFactory();
        chf.execute(xyData);
        chf.execute(xyData);
                System.out.print("Done");
    }
}

这是ChartFactory类:

public class ChartFactory extends Application {

    @Override
    public void stop() throws Exception {
        super.stop();
        Platform.exit();
    }

    private static HashMap<Number, Number> a;

    @Override
    public void start(Stage arg0) throws Exception {
        Platform.setImplicitExit(false);
        // defining the axes
        NumberAxis xAxis = new NumberAxis();
        NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");


        // creating the chart
        LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
        lineChart.setTitle("Stock Monitoring, 2010");
        lineChart.setAnimated(false);

        // defining a series
        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        series.setName("My portfolio");

        // populating the series with data
        for(Map.Entry<Number, Number> entry:a.entrySet() )
            series.getData().add(new XYChart.Data<Number, Number>(entry.getKey(), entry.getValue()));       

        lineChart.getData().add(series);
        lineChart.setPrefSize(800, 240);

        Scene scene = new Scene(lineChart, 800, 240);
        System.out.println(System.getProperty("user.dir"));
        String element = getClass().getResource("chartStyles.css").toExternalForm();
        scene.getStylesheets().add(element);

        WritableImage wi = lineChart.snapshot(new SnapshotParameters(), new WritableImage(800, 240));
        File file = new File("CanvasImage.png");
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
        } catch (Exception s) {
        }

        CategoryAxis xAxis2 = new CategoryAxis();
        xAxis2.setLabel("Devices");
        NumberAxis yAxis2 = new NumberAxis();
        yAxis2.setLabel("Visits");
        BarChart<String, Number> barChart = new BarChart<>(xAxis2, yAxis2);
        barChart.setAnimated(false);

        XYChart.Series<String, Number> dataSeries1 = new XYChart.Series<>();
        dataSeries1.setName("2014");

        dataSeries1.getData().add(new XYChart.Data<String, Number>("Desktop", 178));
        dataSeries1.getData().add(new XYChart.Data<String, Number>("Phone", 65));
        dataSeries1.getData().add(new XYChart.Data<String, Number>("Tablet", 23));

        barChart.getData().add(dataSeries1);
        barChart.setPrefSize(640, 480);

        scene = new Scene(barChart, 640, 480);
        scene.getStylesheets().add(getClass().getResource("chartStyles.css").toExternalForm());
        wi = barChart.snapshot(new SnapshotParameters(), new WritableImage(640, 480));
        file = new File("Canvas2Image.png");
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
        } catch (Exception s) {
        }

        System.out.print("Done!");

        Platform.exit();

    }

    public void execute(HashMap<Number, Number> xyData) {
        a = xyData;
        launch();
    }

}

我想使用不同的数据集生成图表,但次数不确定,但是只能运行一次。

0 个答案:

没有答案