The java.awt.image.BufferedImage class is a subclass of java.awt.Image, introduced in JDK 1.2,
and brings much more flexibility to the way images are manipulated. For the purpose of this tutorial, it is
enough to know that a BufferedImage object has a public constructor and it does not rely on an instance
of a java.awt.Component to be created, as the previous topic describes.
Another important aspect of using BufferedImage is that it is supported by the headless implementation of
JDK 1.4, whereas a Frame instance is not supported. There is a topic in this tutorial explaining how to
configure JDK 1.4 to run an application in a headless environment.
The methods below must always be invoked on the GenericGraph instance when generating offscreen images with
a BufferedImage object:
The example presented in the previous topic was modified to run with a BufferedImage object. The
ChartEncoder class is not used, therefore we need to explicitly invoke one of the availabe encoders to
encode the BufferedImage object into a GIF, JPEG or PNG binary stream.
import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import com.jinsight.jetchart.*; import java.io.*; import java.awt.image.BufferedImage; import java.util.Locale; public class Example2 extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { // Sets the response content type. res.setContentType("image/jpeg"); // If a GIF or PNG format is desired, disable the line above and enable one of the lines below. // res.setContentType("image/gif"); // res.setContentType("image/png"); // Gets a reference to the binary output stream. ServletOutputStream out=res.getOutputStream(); // Creates the chart context. String[] labels={"label1","label2","label3","label4","label5","label6","label7"}; Graph graph=new Graph(labels); graph.setTitle(new String[]{"The JetChart Library","Using BufferedImage"}); // Sets the size of the chart context and enables offscreen graph generation. These two methods // must always be invoked when using JetChart with servlets. graph.setSize(500,350); graph.setOffScreenGraphEnabled(true); // Some JetChart classes place calls to the method Graph.getLocale(), which only returns // a valid Locale object if the Graph object has been laid out on a container, like a // Frame or JFrame object. If the Graph object has not a parent, an exception is then // raised when the Graph.paint(Graphics g) method is invoked to paint chart on the // BufferedImage, telling that Graph must have a parent in order to determine its // locale. If we set the Graph object locale as below, the exception is not raised. graph.setLocale(Locale.getDefault()); // If a BufferedImage object is used to generate offscreen images, the method below // must always be set to true. graph.setBufferedImageEnabled(true,BufferedImage.TYPE_INT_RGB); // Creates a bar series. double[] values={220,180,250,140,100,200,210}; BarSerie bs=new BarSerie(values,"Bar series"); bs.setColor(Color.green); bs.setWidth(15); graph.addSerie(bs); // The BufferedImage object dimension has to be equal to the Graph object dimension. BufferedImage bi=new BufferedImage(500,350,BufferedImage.TYPE_INT_RGB); Graphics g=bi.getGraphics(); graph.paint(g); JpegEncoder jpeg=new JpegEncoder(bi,85,out); jpeg.encode(); // If gif encoding is desired, disable the jpeg encoder and enable the lines below. // GifEncoder gif=new GifEncoder(); // gif.encode(bi,out); // If png encoding is desired, disable the jpeg encoder and enable the lines below. // PngEncoder png=new PngEncoder(); // png.encode(img,out,9); out.close(); } }