save as jpg or gif file

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
kk

save as jpg or gif file

Post by kk » Tue Jan 15, 2002 11:38 pm

It seems I can't save image as jpg or gif files in JFreechart. Does anyone know how I can save image as jpg or gif files or what package I can use to achieve this? Thanks.

David Gilbert

RE: save as jpg or gif file

Post by David Gilbert » Wed Jan 16, 2002 9:36 am

There is a new class ChartUtilities (in JFreeChart 0.7.0) that has methods for saving charts as PNG or JPEG format files. I don't recommend JPEG for charts (JPEG was designed for photographic images), PNG is better for charts - it is a lossless compression algorithm so you don't lose any detail when you compress your charts.

GIF is an older, proprietary, format that does a similar job to PNG, but it is technically inferior to PNG. Most browsers support GIF, some of the older ones don't support PNG.

You choose...

DG.

kk

RE: save as jpg or gif file

Post by kk » Thu Jan 17, 2002 12:31 am

Thanks. Based on your info., I modified the code so I can save it as JPG image -- reason is I want to see it in IE and I can't see PNG in browser. I modified method attemptSaveAs() at class JFreeChartPanel as below,

private void attemptSaveAs() {

JFileChooser fileChooser = new JFileChooser();
ExtensionFileFilter filterPNG = new ExtensionFileFilter("PNG Image Files", ".png");
fileChooser.addChoosableFileFilter(filterPNG);

ExtensionFileFilter filterJPG = new ExtensionFileFilter("JPG Image Files", ".jpg");
fileChooser.addChoosableFileFilter(filterJPG);

int option = fileChooser.showSaveDialog(this);
if (option==JFileChooser.APPROVE_OPTION) {
String fileDesc = fileChooser.getFileFilter().getDescription();
if (fileDesc.startsWith("PNG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("PNG")) ChartUtilities.saveChartAsPNG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsPNG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
} else if (fileDesc.startsWith("JPG")) {
if(!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("JPG")) ChartUtilities.saveChartAsJPEG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".jpg"), this.chart, this.getWidth(), this.getHeight());
else ChartUtilities.saveChartAsJPEG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
}

}//else
}

Locked