Ok, so I created a little example for you to understand better, what my problem is. I found, that there is a createPrintJob() method in ChartPanel. But this method does (of course) not work with my paintComponents, but with the parent one from ChartPanel itself. Therefore the additional data is not shown. I tried to overwrite createPrintJob() and also the print() method (you can find my attempt commented out in the APChartPanel.java at lines 33-56), which works, but has some problems with the drawing area (the plot does not fit the page). But I think, it must be something like this...
So my Demo consists of two classes,
- PrintPlotDemo.java, which contains the main() and creates the stuff.
- APChartPanel.java, which extends ChartPanel to draw some additional stuff onto the plot
You also have to add jfreechart-1.0.13.jar and jcommon-1.0.16.jar to the project. (I guess that this is not so much dependent on the version of jfreechart)
And here is the code:
Code: Select all
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class PrintPlotDemo {
private static final int N = 10;
private static final int Nsamp = 100;
private static final double xMin = 0, xMax = 2 * Math.PI;
public static void main(String[] args) {
XYSeries[] plots = new XYSeries[N];
XYSeriesCollection data = new XYSeriesCollection();
for (int i = 0; i < N; i++) {
plots[i] = new XYSeries("Series " + i);
for (double t = 0; t <= 2 * Math.PI; t += (xMax - xMin) / (Nsamp - 1)) {
plots[i].add(t, Math.sin(t * i)*Math.exp(-3*t/xMax), false);
}
data.addSeries(plots[i]);
}
JFreeChart chart = ChartFactory.createXYLineChart("", "", "", data, PlotOrientation.VERTICAL, false, false, false);
final APChartPanel chartPanel = new APChartPanel(chart);
chartPanel.setAdditional(new String[] {"This is some", "additional data", "which consists of", "some silly strings"});
JButton printButton = new JButton("Print");
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chartPanel.createChartPrintJob();
}
});
JFrame frame = new JFrame("Print Plot Demo");
frame.setLayout(new BorderLayout());
frame.add(chartPanel, BorderLayout.CENTER);
frame.add(printButton, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(800, 600);
}
}
Code: Select all
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
public class APChartPanel extends ChartPanel {
private static final long serialVersionUID = 1L;
private String[] additionalStr = {};
public APChartPanel(JFreeChart chart) {
super(chart);
}
public void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
drawAdditionalStuff(g, getScreenDataArea());
}
public void setAdditional(String[] stuff) {
additionalStr = stuff;
updateUI();
}
// uncomment to get the additional stuff printed, but then, the printed "image" does not fit the page...
// @Override
// public int print(Graphics g, PageFormat pf, int pageIndex) {
// if (pageIndex > 0) {
// return NO_SUCH_PAGE;
// } else {
// Graphics2D g2d = (Graphics2D) g;
// g2d.translate(pf.getImageableX(), pf.getImageableY());
// this.paint(g2d);
// return PAGE_EXISTS;
// }
// }
//
// public void createChartPrintJob() {
// PrinterJob job = PrinterJob.getPrinterJob();
// job.setPrintable(this);
// if (job.printDialog()) {
// try {
// job.print();
// } catch (PrinterException e) {
// JOptionPane.showMessageDialog(this, e);
// }
// }
// }
private void drawAdditionalStuff(Graphics g, Rectangle2D plotArea) {
// Rectangle2D plotArea = getScreenDataArea();
if (additionalStr.length > 0) {
// g.setFont(new Font(null, Font.PLAIN, 10));
FontMetrics met = g.getFontMetrics();
int border = 10, margin = 5;
int x = (int) plotArea.getX(), y = (int) plotArea.getY();
int w = (int) plotArea.getWidth(), h = (int) plotArea.getHeight();
x += w * 3 / 4 - border;
y += border;
w = w / 4;
h = 2 * margin + (additionalStr.length - 1) * (met.getHeight() + 3) + met.getAscent() + 3;
g.setColor(new Color(0.3f, 0.5f, 0.7f, 0.3f));
g.fillRect(x, y, w, h);
g.setColor(new Color(0f, 0f, 0f, 0.6f));
g.drawRect(x, y, w, h);
g.setColor(new Color(0f, 0f, 0f, 0.8f));
int asc = met.getAscent();
int height = met.getHeight();
int xS = x + margin, yS = y + margin + asc, tJ = 2 * margin, hJ = height + 3;
for (int i = 0; i < additionalStr.length; i++)
g.drawString(additionalStr[i], xS + tJ, yS + i * hJ);
}
}
}
I would be really glad, if you could help me. I always dodged around this printing stuff so far, now I want to understand it... ^^