【问题标题】:Fit panel on paper在纸上安装面板
【发布时间】:2016-08-21 16:27:28
【问题描述】:

所以我在 1 个面板中有 2 个面板。第一个面板是图表,第二个是一些信息。我想打印包含这两个面板的面板。所以我使用了一些技巧让面板适合纸张,但它并没有完全发挥作用。这是我的打印工作:

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
      }
}

当我打印那个面板时,它看起来像这样。

我应该如何解决它?纸的底部还可以,只是我没有完全截图。

【问题讨论】:

  • 如果底部没问题,那你具体有什么问题?
  • 如果你仔细看,你会发现在纸的正面,数字被剪掉了,还有一点图表被剪掉了
  • 您的getScaleFactorToFit(...) 方法在哪里?您是否对 MadProgrammer 的答案投了赞成票(看起来您借用了这段代码)?
  • 嗯,它就像 MadProgrammer 的答案一样(是的,我投了赞成票)。是一样的方法。 stackoverflow.com/questions/17904518/…

标签: java swing printing charts


【解决方案1】:

不要缩放渲染的图形,这只会恶化resampling 工件,覆盖封闭ComponentgetPreferredSize() 方法并指定所需的大小。使用诸如FlowLayout 之类的布局来排除调整大小。看来您使用的是JFreeChart,相关示例可以在herehere 中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多