VIRICH
VIRICH

Reputation: 177

How to save the graph of JFreeChart in the PNG image?

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));
                    }
                }
            }
        });
    }

Upvotes: 2

Views: 1736

Answers (1)

VIRICH
VIRICH

Reputation: 177

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();
  }
}

Upvotes: 3

Related Questions