I am novice for java & I have tried to search this forum for my problem, but could not find solution.
After learning few days, I have created one program. Where I have create one chart with help of JFree chart.
I have add one button named "Save Chart" & create one event listener of button.
When user will press Save Chart button then image will save.
My event listener function working properly, but I am unable to save chart.
As to sort out error, I find in event listener, I have write code for save image.
In this function one para chart is creating problem. I am writing in eclipse, so I can show error.
Line which is creating problem in code it is below.
Code: Select all
ChartUtilities.saveChartAsPNG(new File(" c:\\chart.png"), chart, 600, 300 );
Code: Select all
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnits;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class mySerial extends JFrame {
private JButton button;
private JButton button1;
private JButton button2;
private JComboBox jc;
private JLabel jl;
private JPanel jp;
public mySerial(){
setLayout(new FlowLayout());
jp = new JPanel();
add(jp);
jl = new JLabel("Select Port");
add(jl);
jc = new JComboBox<String>();
add(jc);
button = new JButton(" Connect1");
button.setBounds(20, 20, 100, 30);
add(button);
button1 = new JButton(" Connect2");
button1.setBounds(140, 20, 100, 30);
add(button1);
graph();
button2 = new JButton("Save Chart");
button2.setBounds(20, 550, 100, 30);
add(button2);
event e = new event();
button2.addActionListener(e);
}
public void graph() {
XYSeries series = new XYSeries("Sensor Reading ");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("Preassure Graph", "Time(Seconds) ","Preassure Reading", dataset);
chart.setBackgroundPaint(null);
chart.setBorderVisible(true);
add(new ChartPanel(chart),BorderLayout.CENTER);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println(" Button Pressed");
try {
ChartUtilities.saveChartAsPNG(new File(" c:\\chart.png"), chart, 600, 300 );
} catch (IOException e1) {
System.err.println("Problem occurred creating chart.");
}
}
}
public static void main(String args[]){
mySerial gui = new mySerial();
gui.setBounds(0, 0, 700, 600);
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
//gui.setSize(1000, 550);
gui.setTitle(" Serial Port" );
gui.setVisible(true);
}
}