Netbeans, Java, and Jfreechart An Introduction For The Complete Novice (Part 1)
Netbeans, Java, and Jfreechart An Introduction For The Complete Novice (Part 1)
Introduction
This set of “papers” consists of an introduction to JFreeChart and NetBeans as an
IDE for Java programming. I attempt herein to ease the learning burden, a little,
compared to the misery that I had to suffer through. We are using JDK 1.5.0_03,
NetBeans 4.1 (for future work, we've gone over to 5.0), and jfreechart-1.0.0-rc1. I hope
its not necessary to mention that all of these programs are free, in the public domain, and
can be downloaded and installed on modern PCs.
1“Plagiarize,
Let no one else's work evade your eyes,
Remember why the good Lord made your eyes,
So don't shade your eyes,
But plagiarize, plagiarize, plagiarize...
Only be sure always to call it please, "research".”
Illustration 1Figure 1
(notice “Libraries” is highlighted, and then following the next menu’s choices, locate the
jar files necessary, wherever you’ve placed them).
Example 1
Example 1 will consist of a cosine plot, in the easiest manner possible (I think).
/**
*
* @author david
*/
import org.jfree.*;
import org.jfree.data.xy.*;//needed for XYSeries, could have called directly
import org.jfree.chart.*;//needed for createXYLineChart
import java.math.*;//needed for cosine
import java.awt.image.*;//needed for Buffered Image
import javax.swing.*;
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new example1().setVisible(true);
}
});
}
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("<=picture");
getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);
fileMenu.setText("File");
openMenuItem.setText("Open");
fileMenu.add(openMenuItem);
saveMenuItem.setText("Save");
fileMenu.add(saveMenuItem);
saveAsMenuItem.setText("Save As ...");
fileMenu.add(saveAsMenuItem);
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.setText("Edit");
cutMenuItem.setText("Cut");
editMenu.add(cutMenuItem);
copyMenuItem.setText("Copy");
editMenu.add(copyMenuItem);
pasteMenuItem.setText("Paste");
editMenu.add(pasteMenuItem);
deleteMenuItem.setText("Delete");
editMenu.add(deleteMenuItem);
menuBar.add(editMenu);
helpMenu.setText("Help");
contentsMenuItem.setText("Contents");
helpMenu.add(contentsMenuItem);
aboutMenuItem.setText("About");
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
pack();
}
In the future this folded code will be omitted. It is generated by the system, either during
initialization or during construction of the graphical interface, and can be folded out of
view for easier comprehension of the program’s text.
To create our code, one starts with a definition (at the bottom, following the
definitions induced by the NetBeans IDE):
public static int n_points = 500;
public static double[] y_of_x,x;//we will plot y(x) versus x
thinking that we will store the plot’s abscissa in a vector. It turns out to be unnecessary,
but so what2. Next, we create an instance of these variables, and attempt to create a
jfreechart series instance. This fails, since we do not have jfreechart available for use yet,
y_of_x = new double[n_points];
x = new double[n_points];
XYSeries series = new XYSeries("Cos(x) versus x");
so we need
import org.jfree.data.xy.*;//needed for XYSeries, could have called directly
or we could have written “org.jfree.data.xy.XYSeries” explicitly. This is easier, but
finding oneself inside the various folders takes some getting used to.
2 In future incarnations, we'll consolidate the code omiting the y_of_x[] vector, but it doesn;t hurt to leave
it in here.
in order to achieve the use of the cosine (and other) functions3.
Where did jLabel1 come from? It came from the “Design” tab's functioning. We
picked out jLabel and then touched the center of our displayed layout, and, lo and behold,
there we are, its all done. If one looks at the “Properties” of jLabel1, one can find its
“Text” and edit it to what one “<=picture”, which makes our output just a little more
professional looking than the default value.
/**
*
* @author david
*/
import org.jfree.*;
import org.jfree.data.xy.*;//needed for XYSeries, could have called directly
import org.jfree.chart.*;//needed for createXYLineChart
3 Or write “java.math.Cos”.
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.plot.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.xy.*;
import java.math.*;//needed for cosine
import java.awt.image.*;//needed for Buffered Image
import javax.swing.*;
import java.awt.Font;
/**
* Creates new form example2
*/
public example2() {
initComponents();
System.out.println("After initComponents");
y_of_x = new double[n_points];
x = new double[n_points];
XYSeries series1 = new XYSeries("Cos(x) versus x");
XYSeries series2 = new XYSeries("Cos^2(x) versus x");
for (int i = 0;i< n_points;i++){//calculate the data to be plotted
y_of_x[i] = Math.cos(i*Math.PI/180);
series1.add((double)i,y_of_x[i]) ;//add the computed values to the series
series2.add((double)i,Math.pow(y_of_x[i],2)) ;
}
XYDataset dataset1 = new XYSeriesCollection(series1);
XYDataset dataset2 = new XYSeriesCollection(series2);
CombinedDomainXYPlot parent = new CombinedDomainXYPlot(new NumberAxis("x-angle
argument"));
XYItemRenderer renderer1 = new StandardXYItemRenderer();
XYItemRenderer renderer2 = new StandardXYItemRenderer();
XYPlot subplot1 = new XYPlot(dataset1,null, new NumberAxis("Cos(x)"),renderer1);
NumberAxis axis1 = (NumberAxis)subplot1.getRangeAxis();
axis1.setTickLabelFont(new Font("Monospaced", Font.PLAIN,7));
axis1.setLabelFont(new Font("SansSerif", Font.PLAIN,7));
axis1.setAutoRangeIncludesZero(false);
parent.add(subplot1,1);
XYPlot subplot2 = new XYPlot(dataset2,null, new NumberAxis(null),renderer2);
NumberAxis axis2 = (NumberAxis)subplot2.getRangeAxis();
axis2.setTickLabelFont(new Font("Monospaced", Font.PLAIN,11));
axis2.setLabelFont(new Font("SansSerif", Font.PLAIN,9));
axis2.setAutoRangeIncludesZero(false);
parent.add(subplot2,1);
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new example2().setVisible(true);
}
});
}
Of course, we do the same with the second series of data points, cos2(x), but this
time we change some properties so that we can tell what we are doing.
Adding javadocs
In the NetBeans IDE, one of the great conveniences is the idea that when one
types “org.”4 a popup window shows up with choices for the next element, such that
clicking on “jfree” and then adding a period yields “org.jfree.” with yet another popup
box, etc.. These boxes are accompanied by explanatory javadocs boxes if the appropriate
foreign (to NetBeans and Java) javadocs have been placed in the appropriate libraries.
After some trial and error, the following worked:
Illustration 3Figure 3
Example2a
We here change the past example to a briefer form:
XYDataset dataset1 = new XYSeriesCollection(series1);
XYDataset dataset2 = new XYSeriesCollection(series2);
CombinedDomainXYPlot parent = new CombinedDomainXYPlot(new DateAxis(""));
XYItemRenderer renderer1 = new StandardXYItemRenderer();
Notice that we've chosen a different scheme for displaying the graph, i.e., using a
ChartPanel:
/* commented out
BufferedImage image = chart.createBufferedImage(400,500);
jLabel1.setIcon(new ImageIcon(image));
this.setSize(500, 600);
**/
ChartPanel myChart = new ChartPanel(chart);
this.setSize(500, 600);
this.setContentPane(myChart);
And we've finished with our introduction. Good luck.