如何将JPanel打印到具有paintComponent覆盖的打印机?

时间:2017-01-26 14:02:36

标签: java

我在JPAnel中使用paintComponent函数创建了一个图形。有没有办法打印这张图?

当我尝试从print函数调用JPanels paintAll时,它反复调用paintComponent。我想物理打印它,即从我的操作系统将其传递给打印机作业。

这是印刷品:

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
        throws PrinterException {
    double scaleX = pageFormat.getImageableWidth() / mainPanel.getWidth();
    double scaleY = pageFormat.getImageableHeight() / mainPanel.getHeight();
    double scale = Math.min(scaleX,  scaleY);
    Graphics2D g2d = (Graphics2D)graphics;
    AffineTransform Tx = g2d.getTransform();

    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g2d.scale(scale, scale);
    System.out.println(scale + " Came inside");
    mainPanel.printAll(g2d);

    g2d.setTransform(Tx);
    return PAGE_EXISTS;
}

//mainPanel class has the method override as below

protected void paintComponent(Graphics g) 
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    ...
    ...
}

0 个答案:

没有答案