Hi,
I am trying to get a gif image out of my JFreechart, with the Acme gif encoder but all i get is a blank (grey empty) image ...
Can anybody help ?
Thanks
Chris
public static void main(String[] args) {
Number[][] data = new Integer[4][3]; // 4 series 3 categories
for (int i=0; i<4;i++)
{
for (int j=0; j<3;j++) data[j] = new Integer(1+i*j);
}
CategoryDataSource myDataSource = new DefaultCategoryDataSource(data);
JFreeChart myChart = JFreeChart.createVerticalBarChart(myDataSource);
// Customizing
myChart.setTitle("Castor User stats");
Plot myPlot = myChart.getPlot();
Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("User names");
Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Data");
DefaultCategoryDataSource d = (DefaultCategoryDataSource)myDataSource;
String[] names = new String[] {"Mb of data", "Rtcopy commands", "Drive Hours", "others"};
d.setSeriesNames(names);
String[] categories = new String[] {"cnickel", "jpbaud", "obarring"};
d.setCategories(categories);
JFreeChartPanel myPanel = new JFreeChartPanel(myChart);
JFrame frame = new JFrame();
frame.getContentPane().add(myPanel);
frame.setSize(600,480);
frame.addNotify();
Image img = frame.createImage(600,480);
if (img == null)
System.out.println("null image");
try{
FileOutputStream out = new FileOutputStream("monimage.gif");
new GifEncoder(img, out).encode();
out.close();
}
catch (Exception e)
{ System.out.println(e);}
System.exit(0);
}
blank image
RE: blank image
I finally sorted it out by adding :
FileOutputStream out = new FileOutputStream("monimage.gif");
BufferedOutputStream bos = new BufferedOutputStream( out );
the problem is that on the image i dont get the series names. So i followed the advice from a message posted on this forum, saying that i should use
myChart.draw()
instead of
myPaneld.draw()
Then I get an error message : IOException : too many colors for a Gif ...
Then i decided to turn to Jpeg :
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( bos ) ;
encoder.encode( image );
Now i get the series names and everything on the picture, but the image is fuzzy and weighs 10 times more than the gif (35k instead of 3k) so i would like to go back to gif.
Can anyone suggest me anything please ?
Thanks a lot
Chris
FileOutputStream out = new FileOutputStream("monimage.gif");
BufferedOutputStream bos = new BufferedOutputStream( out );
the problem is that on the image i dont get the series names. So i followed the advice from a message posted on this forum, saying that i should use
myChart.draw()
instead of
myPaneld.draw()
Then I get an error message : IOException : too many colors for a Gif ...
Then i decided to turn to Jpeg :
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( bos ) ;
encoder.encode( image );
Now i get the series names and everything on the picture, but the image is fuzzy and weighs 10 times more than the gif (35k instead of 3k) so i would like to go back to gif.
Can anyone suggest me anything please ?
Thanks a lot
Chris
RE: blank image
I haven't used the ACME GIF encoder, but I think the problem is that GIF doesn't handle more than 256 colors.
By default, JFreeChart is using the antialiasing feature of Java2D, and I think the antialiasing generates many shades of each color so it is easy to get more that 256 colors in a chart. Use the setAntiAlias(boolean) method in JFreeChart to to turn it off.
The other solution is to use the PNG format for your images. PNG is a superior format in a number of ways, including not having the limited color range that GIF has...and there is a free (LGPL) encoder (see the links page on this site) available for Java.
Regards,
DG.
By default, JFreeChart is using the antialiasing feature of Java2D, and I think the antialiasing generates many shades of each color so it is easy to get more that 256 colors in a chart. Use the setAntiAlias(boolean) method in JFreeChart to to turn it off.
The other solution is to use the PNG format for your images. PNG is a superior format in a number of ways, including not having the limited color range that GIF has...and there is a free (LGPL) encoder (see the links page on this site) available for Java.
Regards,
DG.
RE: blank image
Hi there,
you'll find a solution for your problem in this forum, see the messages :
GifEncoder just hanging at encode() Jason 10-05-00 01:08
RE:GifEncoder just hanging at encode() Private Heath 10-05-00 17:25
RE: GifEncoder just hanging at encode() Jason 10-05-00 19:32
I try it and it seems ok,
Hope that helps you,
Best regards
you'll find a solution for your problem in this forum, see the messages :
GifEncoder just hanging at encode() Jason 10-05-00 01:08
RE:GifEncoder just hanging at encode() Private Heath 10-05-00 17:25
RE: GifEncoder just hanging at encode() Jason 10-05-00 19:32
I try it and it seems ok,
Hope that helps you,
Best regards