1

我尝试了保存此处找到的 png 文件的代码,它可以工作。但是我想保存一个矢量图,所以我把终端改成了:PostscriptTerminal和SVGTerminal。对于 SVGTerminal,输出为空,当我使用 PostscriptTerminal 时,代码挂在p.plot();

ImageTerminal png = new ImageTerminal();
    PostscriptTerminal eps = new PostscriptTerminal();
    SVGTerminal svg = new SVGTerminal();

    File file = new File("D:/plot.eps");
    try {
        file.createNewFile();
        eps.processOutput(new FileInputStream(file));
    } catch (FileNotFoundException ex) {
        System.err.print(ex);
    } catch (IOException ex) {
        System.err.print(ex);
    }

    PlotStyle myPlotStyle = new PlotStyle();
    myPlotStyle.setStyle(Style.POINTS);
    myPlotStyle.setPointType(7); // 7 - circle
    myPlotStyle.setLineType(3); // blue color

    JavaPlot p = new JavaPlot();
    p.setPersist(false);
    p.setTerminal(eps);
    //p.setTitle(algorithm_name+" - "+problem_name, "Arial", 20);
    p.getAxis("x").setLabel("X axis", "Arial", 15);
    p.getAxis("y").setLabel("Y axis","Arial", 15);
    p.setKey(JavaPlot.Key.TOP_RIGHT);

    double[][] front = this.writeObjectivesToMatrix();
    DataSetPlot s = new DataSetPlot(front);
    s.setPlotStyle(myPlotStyle);
    s.setTitle("");
    p.addPlot(s);
    p.plot(); //code hangs here if I use PostscriptTerminal 

    try {
        //ImageIO.write(png.getImage(), "png", file); //works
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write (eps.getTextOutput());
        //Close writer
        writer.close();
    } catch (IOException ex) {
        System.err.print(ex);
    }
4

1 回答 1

1

我设法保存了一个 .eps 文件。我只需要在构造函数中添加一个文件路径就可以了。

PostscriptTerminal eps = new PostscriptTerminal("output.eps");

    PlotStyle myPlotStyle = new PlotStyle();
    myPlotStyle.setStyle(Style.POINTS);
    myPlotStyle.setPointType(7); // 7 - circle
    myPlotStyle.setLineType(3); // blue color

    JavaPlot p = new JavaPlot();
    p.setPersist(false);
    p.setTerminal(eps);
    p.getAxis("x").setLabel("X axis", "Arial", 15);
    p.getAxis("y").setLabel("Y axis","Arial", 15);
    p.setKey(JavaPlot.Key.TOP_RIGHT);

    double[][] front = this.writeObjectivesToMatrix();
    DataSetPlot s = new DataSetPlot(front);
    s.setPlotStyle(myPlotStyle);
    s.setTitle("");
    p.addPlot(s);
    p.plot(); 
于 2014-09-11T14:08:59.303 回答