Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java打印中的重试?_Java_Printing - Fatal编程技术网

Java打印中的重试?

Java打印中的重试?,java,printing,Java,Printing,如果运行以下代码(从),则两个页面将被调用两次: class PrintObject implements Printable { public int print (Graphics g, PageFormat f, int pageIndex) { System.out.println("Page "+pageIndex); Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graph

如果运行以下代码(从),则两个页面将被调用两次:

class PrintObject implements Printable
{
    public int print (Graphics g, PageFormat f, int pageIndex)
    {
        System.out.println("Page "+pageIndex);
        Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graphics on
        // the print pages :

        // A rectangle that shows the printable area of the page, allowing
        // for margins all round. To be drawn on the first page (index = 0).
        Rectangle2D rect = new Rectangle2D.Double(f.getImageableX(),
        f.getImageableY(),
        f.getImageableWidth(),
        f.getImageableHeight());

        // A simple circle to go on the second page (index = 1).
        Ellipse2D circle = new Ellipse2D.Double(100,100,100,100);

        switch (pageIndex)
        {
            case 0 : g2.setColor(Color.black); // Page 1 : print a rectangle
                g2.draw(rect);
                return PAGE_EXISTS;
            case 1 : g2.setColor(Color.red); // Page 2 : print a circle
                g2.draw(circle);
                return PAGE_EXISTS;
            default: return NO_SUCH_PAGE; // No other pages
        }
    }


    public static void main (String[] args)
    {
        // Create an object that will hold all print parameters, such as
        // page size, printer resolution. In addition, it manages the print
        // process (job).
        PrinterJob job = PrinterJob.getPrinterJob();

        // It is first called to tell it what object will print each page.
        job.setPrintable(new PrintObject());

        // Then it is called to display the standard print options dialog.
        if (job.printDialog())
        {
        // If the user has pressed OK (printDialog returns true), then go
        // ahead with the printing. This is started by the simple call to
        // the job print() method. When it runs, it calls the page print
        // object for page index 0. Then page index 1, 2, and so on
        // until NO_SUCH_PAGE is returned.
        try { job.print(); }
        catch (PrinterException e) { System.out.println(e); }
        }
    }
}
我不知道为什么它会在如此简单的情况下这样做,但真正的问题是,我们的代码可以打印一个大的
JTable
,它会在成功之前重试打印500多次。即使是相对较小的
JTable
也会重试11次。我们正在使用
java.awt.print.PrintJob
,在一台具有2G RAM和1G堆(-Xmx1000m)的机器上关闭双缓冲和使用CutePDF进行测试


有人知道什么会导致这么多的重试吗?

我想你说的是它为每个页面调用print(…)方法的频率

不幸的是,没有真正的方法知道它将被调用多少次,更不用说控制它了,因为它是系统JVM的打印子系统

您所能做的就是尽最大努力优化绘图例程,因为它可以被多次调用(甚至过度调用)

您可能还应该查看Graphic2D剪贴簿,它可能会告诉您打印的是整个页面的哪个子集


在我的简单测试中,在Mac上,使用您的代码,每个页面调用print两次。一次使用看起来像是用来了解要打印什么的图形,另一次使用实际的系统图形2d来进行实际渲染。

添加一些日志记录,查看它到底在做什么。什么语句运行了11次?这11次的每个变量看起来像什么?这看起来更像是一个调试请求,而不是关于打印的问题