2

There was a problem with saving the graphics I received in the form of a picture in a folder on the computer. It seems to me that the problem is in the saving method in the picture, but I do not know how to fix the problem. I marked the problem area in the code (saveImage), I hope for your help)

//create Graph
XYSeriesCollection seriesCollection1 = new XYSeriesCollection(series1);
            chart1 = ChartFactory.createXYLineChart("Зависимость скорости полета от t",
                    "Время, с", "Скорость полета, км/ч", seriesCollection1, PlotOrientation.VERTICAL, false, true, false);
            chartPanel1 = new ChartPanel(chart1);
            chartPanel1.setPreferredSize(new Dimension(1300, 480));
            panel.add(chartPanel1);

//saving method in picture
public void saveImage(File file) {
        Rectangle rec = chartPanel1.getBounds();
        BufferedImage img = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
        print(img.getGraphics()); // I think problem here.
        try {
            ImageIO.write(img, "png", file);
            JOptionPane.showMessageDialog(null, "Данное изображение сохранено", "", JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Ошибка сохранения", "", JOptionPane.ERROR_MESSAGE);
        }
    }

//listener
saveImage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == saveImage) {
                    JFileChooser fc = new JFileChooser();
                    int op = fc.showSaveDialog(OpenFIle.this);
                    if (op == JFileChooser.APPROVE_OPTION){
                        String filename = fc.getSelectedFile().getName();
                        String path = fc.getSelectedFile().getParentFile().getPath();

                        int len = filename.length();
                        String ext = "";
                        String file = "";

                        if (len > 4){
                            ext = filename.substring(len - 4, len);
                        }
                        if (ext.equals(".png")){
                            file = path + "\\" + filename;
                        }else {
                            file = path + "\\" + filename + ".png";
                        }
                        saveImage(new File(file));
                    }
                }
            }
        });
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
VIRICH
  • 177
  • 1
  • 14
  • The problem is likely in the method print(). Could you post the code for that also? – ProgrammersBlock May 25 '18 at 00:55
  • 1) Change `} catch (IOException ex) { JOptionPane.showMessageDialog(..` to something ***useful*** like `} catch (IOException ex) { ex.printStacTrace(): JOptionPane.showMessageDialog(..` 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 25 '18 at 06:49
  • 1
    Why not [`ChartUtils.saveChartAsPNG()`](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/ChartUtils.html)? – Catalina Island May 25 '18 at 15:55
  • I did not know about this method. Thanks, now I'll try – VIRICH May 25 '18 at 17:19

1 Answers1

3

The problem was solved in this way

public void saveImage(File file) {
  Rectangle rec = chartPanel1.getBounds();
  BufferedImage img = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
  Graphics g = img.getGraphics();
  chartPanel1.paint(g);
  try {
    ImageIO.write(img, "png", file);
    JOptionPane.showMessageDialog(null, "Данное изображение сохранено", "", JOptionPane.INFORMATION_MESSAGE);
  } catch (IOException ex) {
    ex.printStackTrace();
  }
}
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
VIRICH
  • 177
  • 1
  • 14