GifEncoder just hanging at encode()

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Jason

GifEncoder just hanging at encode()

Post by Jason » Thu Oct 05, 2000 1:08 am

Hi,

I'm having problem with GifEncoder just hanging at encode().
Can some one please tell me what is wrong here?

Thanks


******************************************************************
import java.io.*;
import java.util.*;
import java.math.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

import Acme.JPM.Encoders.GifEncoder;

import com.jrefinery.chart.*;
import com.jrefinery.chart.ui.*;
import com.jrefinery.util.ui.*;


class A2chart
{
public static void main(String[] args)
{
try
{
XYDataSource xyData = createXYDataSource();
JFreeChart myChart = JFreeChart.createTimeSeriesChart(xyData);
StandardTitle title = (StandardTitle)myChart.getTitle();
title.setTitle("ARTSAC II");
myChart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
//myChart.setAntiAlias(false);

Plot myPlot = myChart.getPlot();
Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Seconds");
Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("Hours of Day");
myPlot.setInsets(new Insets(0, 0, 0, 3)); //???????????

frame = new JFrame();
frame.addNotify();

Image image = frame.createImage(500, 500);
if (image == null)
System.out.println("null image");

g = image.getGraphics();

myChart.draw((Graphics2D) g, new Rectangle(500, 500));

//--------------------------------------------------------------------
//frame.setSize(500, 500);
//Swing.centerFrameOnScreen(frame);
//frame.show(); // same as frame.setVisible(true);
//--------------------------------------------------------------------

FileOutputStream output = new FileOutputStream ("./chart.gif");

GifEncoder encoder = new GifEncoder(image, output, true);

//*******************************************************
// HANGING!!!!!!!!!!
//*******************************************************
encoder.encode();

output.flush();
output.close();
}
catch (IOException err)
{
}
finally
{
if (g != null)
g.dispose();
if (frame != null)
frame.removeNotify();
}
} // public void main()

public static Date createDate(int hour, int min, int sec)
{
GregorianCalendar calendar = new GregorianCalendar(2000, 9, 3, hour, min, sec);
return calendar.getTime();
} // private Date createDate(int year, int month, int day)

public static XYDataSource createXYDataSource()
{
Object[][][] data = new Object[][][] { {

{ createDate( 0, 0, 18), new Double( 0.1) },
{ createDate( 0, 12, 56), new Double( 0.9) },
{ createDate( 0, 18, 34), new Double( 1.8) },
{ createDate( 0, 55, 52), new Double( 2.1) },
{ createDate( 1, 1, 40), new Double( 0.3) },
{ createDate( 1, 1, 49), new Double( 1.2) },
{ createDate( 2, 1, 9), new Double(13.1) },
{ createDate( 2, 1, 20), new Double(10.4) },
{ createDate( 3, 1, 39), new Double( 3.8) },
{ createDate( 4, 1, 11), new Double( 8.1) },
{ createDate( 4, 31, 48), new Double( 1.9) },
{ createDate( 5, 10, 01), new Double( 2.9) },
{ createDate( 5, 20, 11), new Double( 2.0) },
{ createDate( 5, 50, 01), new Double( 3.9) },
{ createDate( 6, 18, 44), new Double( 0.2) },
{ createDate( 6, 38, 44), new Double( 0.6) },
{ createDate( 7, 38, 44), new Double( 0.8) },
{ createDate( 8, 1, 19), new Double( 0.22) },
{ createDate( 9, 29, 28), new Double( 0.3) },
{ createDate(10, 43, 8), new Double( 2.8) },
{ createDate(13, 31, 34), new Double( 3.3) },
{ createDate(17, 52, 42), new Double( 4.9) },
{ createDate(18, 9, 22), new Double( 1.11) },
{ createDate(19, 21, 13), new Double( 9.8) },
{ createDate(22, 1, 19), new Double( 2.4) },
{ createDate(23, 19, 39), new Double( 0.7) } } };

return new DefaultXYDataSource(new String[] { "Max" }, data);
} // public XYDataSource createXYDataSource()

private static JFrame frame = null;
private static Graphics g = null;
} // public class A2chart

Private Heath

RE: GifEncoder just hanging at encode()

Post by Private Heath » Thu Oct 05, 2000 5:25 pm

Jason,
I copied you program and tried it myself... I had the GIF picture writing to my file. I then placed some System.out.println("markers") to help me follow your path.(Because could not see anything wrong). And infact on my machine it was not wrong it appears not to get hung up at the encode();

I then just added a System.exit(0); at...

finally
{
if (g != null)
g.dispose();
if (frame != null)
frame.removeNotify();
}
System.exit(0);
} // public void main()

Now the program exits(Hope this is what you wanted.. Ie does not appear to be getting hung)

Cheers
Private Heath

Jason

RE: GifEncoder just hanging at encode()

Post by Jason » Thu Oct 05, 2000 7:32 pm

Actually, my problem turned out to be 256 color for
gif image. I uncommented below line and it
works fine now.

Thanks.

//myChart.setAntiAlias(false);

Post Reply