Can't create an image with jfreechart

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

Can't create an image with jfreechart

Post by Claudio Parnenzini » Mon Oct 16, 2000 2:50 pm

Hi all,

Someone can explain to me why i can't get an Graphics2D image of my chart. I'm receiving a null pointer exception when I try to get a Graphics2D (or graphics ) object.

Thanks


/**
* Title: <p>
* Description: <p>
* Copyright: Copyright (c) <p>
* Company: <p>
* @author
* @version 1.0
*/
package firstchart;

import java.io.*;

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

import java.awt.*;
import java.awt.image.BufferedImage;
//import com.sun.media.jai.codec.*;
import com.sun.image.codec.jpeg.*;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;

public class createJpg
{
CategoryDataSource myDataSource;
JFreeChart myChart;
JFreeChartPanel myChartPanel;
Plot myPlot;
Axis myVerticalAxis, myHorizontalAxis;
DefaultCategoryDataSource d;
String[] names, categories;
FileOutputStream out;


Number data[][] = new Integer[][]{
{new Integer(10000),new Integer(10000),new Integer(20000),new Integer(20000)},
{new Integer(30000),new Integer(30000),new Integer(40000),new Integer(40000)},
{new Integer(40000),new Integer(30000),new Integer(50000),new Integer(50000)}
};

File file = new File("/tmp/chart.jpg");

public void createChart()
{
System.out.println("IN createChart()");
myDataSource = new DefaultCategoryDataSource(data);
myChart = JFreeChart.createVerticalBarChart(myDataSource);
myChartPanel = new JFreeChartPanel(myChart);

myChart.setTitle("My First chart");

myPlot = myChart.getPlot();
myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("X axis");

myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Y axis");

d = (DefaultCategoryDataSource)myDataSource;
names = new String[]{"Region 1","Region2","Region3"};
d.setSeriesNames(names);
categories = new String[]{"Quarter1", "Quarter2", "Quarter3", "Quarter4"};
d.setCategories(categories);
myChart.setChartBackgroundPaint(new GradientPaint(0,0,Color.white,0,1000,Color.orange));
System.out.println("Calling createImageFile");
createImageFile();
System.out.println("OUT createChart");
}

public void createImageFile()
{
System.out.println("IN createImageFile");
if (file.exists()) {file.delete();}
try {file.createNewFile();} catch (IOException e2){System.out.println(e2);}

try
{
out = new FileOutputStream( file );

BufferedImage image = (BufferedImage)myChartPanel.createImage(myChartPanel.getWidth(),myChartPanel.getHeight());
//Image image = (Image)myChartPanel.createImage(myChartPanel.getWidth(),myChartPanel.getHeight());

if(image.equals(null)){System.out.println("Image null");}

-----> here is the null pointer
Graphics2D graphics = (Graphics2D)image.getGraphics();
-----> End of the null pointer

myChartPanel.paint(graphics);
System.out.println("Here");
if(graphics != null){myChartPanel.paintAll(graphics);}
else System.out.println("Graphics null");

JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image,encodeParam);

//RenderedOp jaiImage = JAI.create("AWTImage", image);
//RenderedOp op = JAI.create("filestore",jaiImage,"chart.jpg","JPEG");

}
catch(IOException e){System.out.println(e);}

try
{
out.flush();
out.close();
}catch(IOException e){System.out.println(e);}
System.out.println("OUT createImageFile()");
}
}

/*File file = new File("/usr/home/claudio/chart.gif");
if (file.exists()) {file.delete();}

try {file.createNewFile();} catch (IOException e2) {} try
{
OutputStream out = new FileOutputStream( file );
// Create the image get its grapics and paint them with the Chart(or any Jpanel/container)
Image image = (Image)myChartPanel.createImage(myChartPanel.getWidth(),myChartPanel.getHeight());
Graphics g = image.getGraphics();
myChartPanel.paint(g);
GifEncoder encoder = new GifEncoder( image, out );
encoder.encode();
out.flush();
out.close();
}
catch(FileNotFoundException e2){System.out.println(e2);} catch(IOException e2){System.out.println(e2);}
*/

GianLuca Paloni

RE: Can't create an image with jfreechart

Post by GianLuca Paloni » Wed Oct 25, 2000 3:19 pm

Ciao, immagino tu sia italiano.

Ho provato la stessa identica cosa con una servlet ed ho avuto analogo risultato...!! Ho anche provato ad usare il metodo: draw(Graphics2D) della classe JFreeChart....stesso esito!
A questo punto penso che sia un baco della classe!

Se hai notizie in proposito, ti prego di farmi sapere (e viceversa)
Grazie,
Gian Luca Paloni

GianLuca Paloni

RE: Can't create an image with jfreechart

Post by GianLuca Paloni » Thu Oct 26, 2000 11:13 am

Trovato!

Utilizza la funzione draw(Graphics2D, Rectangle2D) applicata non all'oggetto JFreeChart ma al plot (yourChart.getPlot().draw(..))
A me così funziona.....Auguri.

Bob Raker

RE: Can't create an image with jfreechart

Post by Bob Raker » Sat Nov 04, 2000 7:32 pm

Here is some sampe code that I am using and it works.

XYDataSource xyData1 = createTestXYDataSource();
JFreeChart chart = JFreeChart.createTimeSeriesChart(xyData1);
StandardTitle title = (StandardTitle)chart.getTitle();
title.setTitle("Value of GBP");
//chart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white,0, 1000, Color.blue));
Plot myPlot = chart.getPlot();
Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("USD per GBP");

BufferedImage image = new BufferedImage( 300, 200, BufferedImage.TYPE_INT_RGB );
Graphics2D g = image.createGraphics();
g.setColor( Color.white );
g.fillRect(0, 0, 300, 200 );
g.setColor( Color.orange );
g.drawRect(0, 0, 290, 190 );
//g.setBackground( Color.lightGray );
/*g.setColor( Color.blue );
g.fillRect(100, 50, 400, 400 );
g.setColor( Color.red );
g.drawOval( 150, 100, 300, 100 );*/

Rectangle drawArea = new Rectangle( 300, 200 );
myPlot.draw( g, drawArea );

try {
FileOutputStream fos = new FileOutputStream( "c:\java\test.jpeg" );
BufferedOutputStream bos = new BufferedOutputStream( fos );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( bos ) ;
encoder.encode( image );
bos.close();
} catch( FileNotFoundException e ) {
System.out.println( "file not found " + e.getMessage() );
System.exit(1);
} catch( IOException e ) {
System.out.println( "IOException " + e.getMessage() );
System.exit(1);
}

I am having troubles getting the title and legend to show but I do get an image created and no exceptions.

Locked