Пока вот что у меня есть:
// вкладка представляет собой JTabbedPane
for(int i = 0; i < tab.getTabCount(); i ++){
if(tab.getComponent(i) instanceof DuctReport)
PrintUtilities.printComponent(tab.getComponent(i), DuctReport.PRINT_SIZE);
else if(tab.getComponent(i) instanceof Vandy)
PrintUtilities.printComponent(tab.getComponent(i), Vandy.PRINT_SIZE);
else
PrintUtilities.printComponent(tab.getComponent(i), .8);
}
// вот код для PrintUtilities:
public class PrintUtilities implements Printable{
private Component componentToBePrinted;
private double scale;
public static void printComponent(Component c,double scale) {
new PrintUtilities(c,scale).print();
}
public PrintUtilities(Component componentToBePrinted, double scale) {
this.componentToBePrinted = componentToBePrinted;
this.scale = scale;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.scale(scale, scale);
g2d.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
}
Когда я делаю то, что у меня есть, я должен индивидуально нажимать «печать» для каждого компонента, который должен быть напечатан, какие-либо идеи о том, что я могу сделать, чтобы напечатать все компоненты в одной печати?
Мне также нужно центрировать компонент на странице, я попытался сделать:
g2d.translate((pageFormat.getImageableWidth()-componentToBePrinted.getWidth)/2,
pageFormat.getImageableY());
но в некоторых случаях ширина компонента больше ширины изображения, есть идеи?