JMSL Chart
Programmer's Guide
Writing a Chart as a Bitmap Image File  Previous Page  Contents  Next Page

Writing a Chart as a Bitmap Image File

A JMSL chart can be saved as an image file in two ways: either using the Java ImageIO class or the Java Advanced Imaging (JAI) API.

Using the ImageIO Class

The ImageIO class was added to Java in JDK1.4. The JAI, descibed in the next section, can be used with older versions of Java.

The chart tree is constructed in the usual manner. Here a method called
createChart is used to create a simple chart, with the null-argument Chart constructor.

The bitmap is generated by creating a BufferedImage object and painting the chart into the buffered image using the graphics object from the buffered image. The buffered image is written to a file using the ImageIO.write method.

This method of creating bitmaps does not require that a windowing system be running. The argument -Djava.awt.headlesss=true can be used with the java command from JDK1.4, or later, to run Java in a "headless" mode.


(Download Code)

import com.imsl.chart.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class SampleImageIO {
    public static void main(String argv[]) throws java.io.IOException {        
        Chart chart = createChart();
        chart.setScreenSize(new java.awt.Dimension(500,500));
        
        BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        chart.paintChart(bi.createGraphics()); 
        
        File file = new File("SampleImageIO.png");
        javax.imageio.ImageIO.write(bi, "PNG", file);       
    }
    

    static Chart createChart() {        
        Chart chart = new Chart();
        AxisXY axis = new AxisXY(chart);      
        
        int  npoints = 20;
        double dx = .5 * Math.PI/(npoints-1);
        double x[] = new double[npoints];        
        double y[] = new double[npoints];       

        //  Generate some data       
        for (int i = 0; i < npoints; i++){            
            x[i] = i * dx;            
            y[i] = Math.sin(x[i]);            
        }        
        new Data(axis, x, y);   
        return chart;
    }    
}

Using the Java Advanced Imaging API

A JMSL chart can be saved as an image file by generating a java.awt.Image object and then rendering the image using the Java Advanced Imaging (JAI) API. The example below shows the steps needed.

JAI is not included with JMSL Numerical Library. It is a standard Java extension that can be downloaded, for free, from Sun's JAI download site.

The first step is to create a java.awt.Frame object. A Frame object is required because the implementation of the drawing code is based on the code used to write to the screen. Because a Frame object is required, a windowing system must be running on the system. The size of the frame, here 500 by 500, determines the size of the bitmap image.

The chart tree is then constructed in the usual manner. Here a method called
createChart is used to create a simple chart, with the Frame object passed to the Chart constructor.

An Image object is created using the paintImage method in Chart.

The create method in javax.media.jai.JAI can now be used to generate the file. The first call (with AWTImage) is used to generate a JAI object from the AWT Image object. The second call (with filestore) actually writes the file. In this case we write a PNG format file called SampleJai.png. JAI can be used to generate image files in other formats, but PNG is usually the best for charts.

The last step is to exit explicitly. The program does not automatically exit because of the existence of a user thread, implicitly created to handle the frame.

(Download Code)
import com.imsl.chart.*;
import java.awt.*;
import javax.media.jai.*;

public class SampleJai {
    public static void main(String argv[]) {        
        Frame frame = new Frame();
        frame.show();
        frame.setSize(500, 500);
        frame.setVisible(false);        
        Chart chart = createChart(frame);  

        Image image = frame.createImage(500, 500);
        chart.paintChart(image.getGraphics());  
        RenderedOp im = JAI.create("AWTImage", image);        
        JAI.create("filestore", im,  "SampleJai.png", "PNG");        
        System.exit(0);
    }

    
    static Chart createChart(Component component) {        
        Chart chart = new Chart(component);        
        AxisXY axis = new AxisXY(chart);      
        
        int  npoints = 20;
        double dx = .5 * Math.PI/(npoints-1);        
        double x[] = new double[npoints];        
        double y[] = new double[npoints];       

        //  Generate some data       
        for (int i = 0; i < npoints; i++){            
            x[i] = i * dx;            
            y[i] = Math.sin(x[i]);            
        }        
        new Data(axis, x, y);        
        return chart;        
    }    
}

Using the Scalable Vector Graphics (SVG) API

Scalable Vector Graphics (SVG) is an XML vocabulary for vector graphics.

JMSL's SVG support is based on the Apache Batik toolkit. Batik is not included with JMSL Numerical Library. It can be downloaded, for free, from the Apache Batik site. The batik.jar file needs to be added to the CLASSPATH.

Once batik.jar is installed, a JMSL chart can be saved as an SVG file by using the Chart.writeSVG method.

SVG can also be create from a JFrameChart window. Its File | SaveAs menu allows the chart to be saved as either a PNG image or as an SVG file. The PNG option is active only if J2SE 1.4 or later is being used, or if the JAI classes are in the CLASSPATH. The SVG option is active only if batik.jar is in the CLASSPATH.

The following example generates SVG output:

The output encoding format is set to UTF-8. This is done because SVG renders do not support all encodings. The UTF-8 is the most general encoding.

(Download Code)
 
import com.imsl.chart.*;
import java.io.*;
import java.awt.*;

public class SampleSVG {
    public static void main(String argv[]) throws java.io.IOException {        
        Frame frame = new Frame();
        frame.show();
        frame.setSize(500, 500);
        frame.setVisible(false);        
        Chart chart = createChart(frame);
        
        OutputStream os = new FileOutputStream("SampleSVG.svg");
        Writer writer = new OutputStreamWriter(os, "UTF-8");
        chart.writeSVG(writer, true);
       
        System.exit(0);
    }

    
    static Chart createChart(Component component) {        
        Chart chart = new Chart(component);        
        AxisXY axis = new AxisXY(chart);      
        
        int  npoints = 20;
        double dx = .5 * Math.PI/(npoints-1);        
        double x[] = new double[npoints];        
        double y[] = new double[npoints];       

        //  Generate some data       
        for (int i = 0; i < npoints; i++){            
            x[i] = i * dx;            
            y[i] = Math.sin(x[i]);            
        }        
        new Data(axis, x, y);        
        return chart;        
    }    
}




©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page