gpt4 book ai didi

java - java 中的打印在 Windows 7 中无法完成

转载 作者:行者123 更新时间:2023-12-02 05:04:27 24 4
gpt4 key购买 nike

当我在 Windows 7 中通过 Java 应用程序打印某些内容时,输出是空白页,并且仅打印最后一个图形,但是当我在 Windows 10 中打印它时,它打印成功..所以我不知道为什么在 Windows 7 中会发生这种情况特别是它之前在 Windows 7 上打印,突然发生了我之前所说的情况(空白页)

这是我的代码和下图中的输出

    public void printed_bill_printing() {
totals_panelB.setVisible(true);
footer_panelB.setVisible(true);

int count_rows = tableEB.getRowCount();
int count = 0;
int table_height = 0;
while (count <= count_rows - 1) {
int row_height = tableEB.getRowHeight(count);
table_height = row_height + table_height;
count++;
}


float widtha = scrollPaneEB.getWidth();
float heighta = table_height + 30;

scrollPaneEB.setSize(Math.round(widtha), Math.round(heighta));

PrinterJob printJob = PrinterJob.getPrinterJob();
final int finalTable_height = table_height;
printJob.setPrintable(new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}

Graphics2D paint_PF = (Graphics2D) graphics;
paint_PF.scale(.68, .6);
B.print(paint_PF);

Graphics2D paint_table = (Graphics2D) graphics;
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);

Graphics2D paint_egmaly = (Graphics2D) graphics;
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);


Graphics2D paint_tawke3 = (Graphics2D) graphics;
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);

return Printable.PAGE_EXISTS;
}
});

PrintRequestAttributeSet printRequestAttrSet = new HashPrintRequestAttributeSet();
printRequestAttrSet.add(new MediaPrintableArea(0, 0, 350, 500, MediaPrintableArea.MM));
printRequestAttrSet.add(Chromaticity.MONOCHROME);

try {
PrintService ps = findPrintService(String.valueOf(printer1PST_combo.getSelectedItem()));
printJob.setPrintService(ps);
printJob.print(printRequestAttrSet);
} catch (PrinterException ex) {

}
adapt_table_size(scrollPaneEB, 440, 351);

}

enter image description here

最佳答案

转换 Graphics 对象不会创建新的 Graphics 对象。您重复转换 Graphics 对象并将其放入新变量中,但它仍然是相同的 Graphics 对象,因此您的更改是累积的。

单步执行代码:

Graphics2D paint_PF = (Graphics2D) graphics;

这会导致paint_PF 包含传递给该方法的相同的打印Graphics 对象。它将图形对象称为 Graphics2D,但它仍然是同一个对象。

paint_PF.scale(.68, .6);
B.print(paint_PF);

现在,原始打印 Graphics 对象的比例为 0.68 x 0.6。

Graphics2D paint_table = (Graphics2D) graphics;

这又是同一个 Graphics 对象,代码刚刚将其设置为 0.68 x 0.6 比例。

paint_table.translate(1, 430);
paint_table.scale(.68, 1);

打印 Graphics 对象仍然按 0.68 乘 0.6 缩放;现在您将其进一步缩放 0.68,从而生成一个 Graphics 对象,该对象实际上缩放了 0.04624 0.6(因为 0.68 × 0.68 = 0.04624)。

scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);

Graphics2D paint_egmaly = (Graphics2D) graphics;

paint_egmaly 仍然包含原始打印 Graphics 对象。

paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);

现在您的打印图形已经被缩放、再次缩放、翻译、再次缩放。

totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);


Graphics2D paint_tawke3 = (Graphics2D) graphics;

paint_tawke3 包含原始图形对象,以及之前的所有缩放和平移。

paint_tawke3.translate(0, 200);

现在 Graphics 对象已经被缩放、缩放、平移、缩放、再次平移。

footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);

临时更改 Graphics 的一种方法是使用 Graphics.create 创建一个新的 Graphics 对象。 ,然后应用您的更改。如果这样做,您将负责通过调用 Graphics 对象的 dispose 来释放该对象。方法。

Graphics2D paint_PF = (Graphics2D) graphics.create();
paint_PF.scale(.68, .6);
B.print(paint_PF);
paint_PF.dispose(); // DO NOT FORGET THIS

Graphics2D paint_table = (Graphics2D) graphics.create();
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
paint_table.dispose(); // DO NOT FORGET THIS

Graphics2D paint_egmaly = (Graphics2D) graphics.create();
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
paint_egmaly.dispose(); // DO NOT FORGET THIS


Graphics2D paint_tawke3 = (Graphics2D) graphics.create();
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
paint_tawke3.dispose(); // DO NOT FORGET THIS

另一个使用较少资源的选项是在每次更改后恢复 Graphics 对象的变换:

AffineTransform originalTransform = ((Graphics2D) graphics).getTransform();

Graphics2D paint_PF = (Graphics2D) graphics;
paint_PF.scale(.68, .6);
B.print(paint_PF);
paint_PF.setTransform(originalTransform);

Graphics2D paint_table = (Graphics2D) graphics;
paint_table.translate(1, 430);
paint_table.scale(.68, 1);
scrollPaneB.setVisible(false);
scrollPaneEB.print(paint_table);
paint_table.setTransform(originalTransform);

Graphics2D paint_egmaly = (Graphics2D) graphics;
paint_egmaly.translate(1, finalTable_height + tableEB.getTableHeader().getHeight() + 2);
paint_egmaly.scale(1.45, 1);
totals_panelB.setVisible(false);
totals_panelB.print(paint_egmaly);
paint_egmaly.setTransform(originalTransform);


Graphics2D paint_tawke3 = (Graphics2D) graphics;
paint_tawke3.translate(0, 200);
footer_panelB.setVisible(false);
footer_panelB.print(paint_tawke3);
paint_tawke3.setTransform(originalTransform);

关于java - java 中的打印在 Windows 7 中无法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56362054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com