Reputation: 173
So I have 2 panel in 1 panel. On 1 panel is chart, and on second is some information. I want to print that panel witch contains that two panel. So I used some tricks for panel to fit to paper, but it didn't work fully. Here is my printing job:
public void printComponenet(Component comp){
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName(" Print Component ");
PageFormat pf = pj.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
pj.setPrintable (new Printable() {
public int print(Graphics g, PageFormat pf, int pageNum){
if(pageNum > 0)
{
return Printable.NO_SUCH_PAGE;
}
Dimension compSize = comp.getPreferredSize();
comp.setSize(compSize);
Dimension printSize = new Dimension();
printSize.setSize(pf.getImageableWidth(), pf.getImageableHeight());
double scaleFactor = getScaleFactorToFit(compSize, printSize);
if(scaleFactor > 1d)
{
scaleFactor = 1d;
}
double scaleWidth = compSize.width * scaleFactor;
double scaleHeight = compSize.height * scaleFactor;
Graphics2D g2 = (Graphics2D) g.create();
double x = ((pf.getImageableWidth() - scaleWidth) / 2d) + pf.getImageableX();
double y = ((pf.getImageableHeight() - scaleHeight) / 2d) + pf.getImageableY();
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.scale(scaleFactor, scaleFactor);
g2.transform(at);
comp.printAll(g2);
g2.dispose();
comp.revalidate();
return Printable.PAGE_EXISTS;
}
}, pf);
if (pj.printDialog() == false)
return;
try {
pj.print();
} catch (PrinterException ex) {
// handle exception
}
}
And when I print that panel, it looks like this.
How should I fix it? Bottom side of paper is okay, I just didn't screenshoted it fully.
Upvotes: 0
Views: 89
Reputation: 205875
Instead of scaling the rendered graphics, which will only worsen resampling artifact, override the getPreferredSize()
method of the enclosing Component
and specify the desired size. Use a layout such as FlowLayout
to preclude resizing. As it appears that you are using JFreeChart
, related examples may be found here and here.
Upvotes: 1