Hi folks,
I have a verticalBarChart in a chartPanel which is added to a JPanel 'jPanelChart' which is added to the contentpane.
Now I need to print the chart to a paper.
I do...
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
[using PrintUtilities.java provided by Marty Hall
http://www.apl.jhu.edu/~hall/java/Swing ... nting.html]
...and print the contentPane. Everything works fine except for the chart. The printout of my chart is not quite clear and i cannot read any numbers, titles or the legend.
Has this something to do with Graphics2D?
I'm trying to convert the chart to an image and adding this to the contentpane, but without success (so far...).
Does anyone know how to print the chart? Is there a sample available? I've searched the forums threads and also JFreeChart demos but found nothing.
Any help is appreciated!
Thanks!
Lisa
Printing the chart
Re: Printing the chart
In the JFreeChartDemo, you should be able to select "Print..." from the popup menu (right mouse click on the chart).
This ends up calling the print(...) method in the ChartPanel class, which grabs the Graphics2D for the printer then draws the chart on it. The quality is good on my printer.
Without trying out your code, I'm guessing that it prints an image of the JComponent that contains the chart...that means that what you see on paper will have the same resolution as your screen, which won't look good.
I hope that helps, I don't know a whole lot about the Java printing API...
Regards,
DG.
This ends up calling the print(...) method in the ChartPanel class, which grabs the Graphics2D for the printer then draws the chart on it. The quality is good on my printer.
Without trying out your code, I'm guessing that it prints an image of the JComponent that contains the chart...that means that what you see on paper will have the same resolution as your screen, which won't look good.
I hope that helps, I don't know a whole lot about the Java printing API...
Regards,
DG.
Re: Printing the chart
Hello David,
thanks for your response!
Printing the chart alone is fine, but I need to print the chart with some other components of my contentpane. I have another legend in a 'normal' JPanel and need to print both chart AND JPanel to the same paper.
How can I do this?
Adding the JPanel to chartPanel didn't work for unknown reasons...
I also tried to add the JPanel to the ChartPanel print(...)-method, but no success.
When I print the contentpane, I get the ugly chart (think you're right with your guess)...
Any suggestions?
TIA,
Lisa
BTW: Congrats for JFreeChart, David; Really great work!
thanks for your response!
Printing the chart alone is fine, but I need to print the chart with some other components of my contentpane. I have another legend in a 'normal' JPanel and need to print both chart AND JPanel to the same paper.
How can I do this?
Adding the JPanel to chartPanel didn't work for unknown reasons...
I also tried to add the JPanel to the ChartPanel print(...)-method, but no success.
When I print the contentpane, I get the ugly chart (think you're right with your guess)...
Any suggestions?
TIA,
Lisa
BTW: Congrats for JFreeChart, David; Really great work!
Re: Printing the chart
Ok, finally I got it working (although I still don't know, why I can't add my JPanel to chartPanel...)
I'm now able to print both chart and JPanel on the same paper.
I print the JPanel and draw the chart (in the JPanel-print()-method) on the JPanel-Graphics2D.
While printing, the following problems occur:
1. After pressing 'OK' on the print-dialog, the print-dialog closes, but my application is out of focus now. (I think I can get this solved by calling getFocus in my application; I didn't try yet)
2. My printer displays something like 'no paper found, go and fill the tray', but there IS paper in the tray, so I press the button 'Continue' and my printer prints without adding paper. I have no clue about this. Why is this so?? This only occurs in my application.
3. My printer needs enormous time to start printing and displays ('MTR compression'). I think this has something to do with doubleBuffering as, when I comment out chart.draw(...) and do setDoubleBufferingEnabled(false) for my JPanel, my printer prints immediately. But I cannot turn off doubleBufferingEnabled for the chart or the Graphics2D as they are not components and I can't get the RepaintManager for them (which is needed to disable doubleBuffering).
Any ideas on this?
Any help is appreciated!
Thanks in advance!
Lisa
I'm now able to print both chart and JPanel on the same paper.
I print the JPanel and draw the chart (in the JPanel-print()-method) on the JPanel-Graphics2D.
While printing, the following problems occur:
1. After pressing 'OK' on the print-dialog, the print-dialog closes, but my application is out of focus now. (I think I can get this solved by calling getFocus in my application; I didn't try yet)
2. My printer displays something like 'no paper found, go and fill the tray', but there IS paper in the tray, so I press the button 'Continue' and my printer prints without adding paper. I have no clue about this. Why is this so?? This only occurs in my application.
3. My printer needs enormous time to start printing and displays ('MTR compression'). I think this has something to do with doubleBuffering as, when I comment out chart.draw(...) and do setDoubleBufferingEnabled(false) for my JPanel, my printer prints immediately. But I cannot turn off doubleBufferingEnabled for the chart or the Graphics2D as they are not components and I can't get the RepaintManager for them (which is needed to disable doubleBuffering).
Any ideas on this?
Any help is appreciated!
Thanks in advance!
Lisa
Re: Printing the chart
No response?
I would really appreciate any help as I have no idea how to solve 2.) and 3.) ...
1.) The focus-loss can be managed by nextFocus(), so after closing the printDialog my application maximizes again. I do not know how to let my application stay maximized while the printDialog is visible, but this is not so important for now (as long as 2.) and 3.) are not fixed...).
2.) I have found out, that this problem only occurs on my network printer. Normally I can set up the printing options in the printDialog and everything works fine. But setting up the printing options (in the printDialog) does not make a difference when started out of my application.
3.) still no clue about this...
Any help is appreciated!
Hope someone can give me a hint...
Thanks in advance!
Lisa
I would really appreciate any help as I have no idea how to solve 2.) and 3.) ...
1.) The focus-loss can be managed by nextFocus(), so after closing the printDialog my application maximizes again. I do not know how to let my application stay maximized while the printDialog is visible, but this is not so important for now (as long as 2.) and 3.) are not fixed...).
2.) I have found out, that this problem only occurs on my network printer. Normally I can set up the printing options in the printDialog and everything works fine. But setting up the printing options (in the printDialog) does not make a difference when started out of my application.
3.) still no clue about this...
Any help is appreciated!
Hope someone can give me a hint...
Thanks in advance!
Lisa
Re: Printing the chart
Hi,
I don't have any advice at the moment, but I think I have a similar problem. I put my chartPanel in a tabbed container, and then a JFrame, and wanted to be able to print the frame containing the container as "current view", so it printed whatever was displayed at the time.
This worked fine in Java 1.3, but when I upgraded to 1.4 I got the same problem as you with Windows NT, but not with XP. Everything prints fine except the panel containing the graph is poor resolution and not even the correct colors. I also tried to turn off the buffering, and it prints fine then, but it also takes forever to print. All other views of my tabbed container print fine always.
I submitted a bug report to Sun because in my case it seemed to be caused by the upgrade to 1.4, and they think it is a bug in the printer driver. Someone has suggested to me that problem is probably caused by the graph being an awt component added to a Swing based program, and this can lead to many problems with buffering.
Anyway, I am fairly new to java programming, and I am not able to fix this or offer you advice. I think I might just put my graph in it's own frame, and display and print it separately from the rest of my output for now.
Colleen
I don't have any advice at the moment, but I think I have a similar problem. I put my chartPanel in a tabbed container, and then a JFrame, and wanted to be able to print the frame containing the container as "current view", so it printed whatever was displayed at the time.
This worked fine in Java 1.3, but when I upgraded to 1.4 I got the same problem as you with Windows NT, but not with XP. Everything prints fine except the panel containing the graph is poor resolution and not even the correct colors. I also tried to turn off the buffering, and it prints fine then, but it also takes forever to print. All other views of my tabbed container print fine always.
I submitted a bug report to Sun because in my case it seemed to be caused by the upgrade to 1.4, and they think it is a bug in the printer driver. Someone has suggested to me that problem is probably caused by the graph being an awt component added to a Swing based program, and this can lead to many problems with buffering.
Anyway, I am fairly new to java programming, and I am not able to fix this or offer you advice. I think I might just put my graph in it's own frame, and display and print it separately from the rest of my output for now.
Colleen