从Netbeans环境运行正确打印文本。直接运行相同的JAR不会

时间:2014-04-22 18:13:30

标签: java swing netbeans printing

我看到了最不寻常的困境。

我有一个基于摇摆的应用程序。我正在通过此代码打印我的jpanel。

按钮代码

private void printDashButtonMouseClicked(java.awt.event.MouseEvent evt) {                                             
    try {
        if (evt.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(evt)) {
            PrinterJob pjob = PrinterJob.getPrinterJob();
            pjob.setJobName("Print ROP Dash");

            PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
            attr_set.add(Fidelity.FIDELITY_TRUE);
            attr_set.add(PrintQuality.HIGH);
            attr_set.add(OrientationRequested.LANDSCAPE);
            attr_set.add(MediaSizeName.NA_LETTER);

            float width = (float) MediaSize.NA.LETTER.getX(MediaSize.INCH);
            float height = (float) MediaSize.NA.LETTER.getY(MediaSize.INCH);
            attr_set.add(new MediaPrintableArea(0.25f, 0.25f, width - 0.5f, height - 0.5f, MediaPrintableArea.INCH));

            //get the formatter
            PageFormat pf2 = pjob.pageDialog(attr_set);

            //If user does not hit cancel then print.
            if (pf2 != null && pjob.printDialog() == true) {
                //Set print component
                pjob.setPrintable(new Prints(), pf2);

                //print the dash
                pjob.print();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}    

打印代码

//handle the printing
private class Prints implements Printable {
    //convert panel to image

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return Printable.NO_SUCH_PAGE;
        }

        // get the bounds of the component
        Dimension dim = dashHolderPanel.getSize();
        double cHeight = dim.getHeight();
        double cWidth = dim.getWidth();

        // get the bounds of the printable area
        double pHeight = pageFormat.getImageableHeight();
        double pWidth = pageFormat.getImageableWidth();

        double pXStart = pageFormat.getImageableX();
        double pYStart = pageFormat.getImageableY();

        double xRatio = pWidth / cWidth;
        double yRatio = pHeight / cHeight;

        Graphics2D g2 = (Graphics2D) graphics;
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        //g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        //g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2.translate(pXStart, pYStart);
        g2.scale(xRatio, yRatio);
        dashHolderPanel.print(g2);

        return Printable.PAGE_EXISTS;
    }
}

如果我从Netbeans中运行应用程序,那么打印结果就是这个。请注意文本间距很好而且紧凑。这是理想的。

Narrow Text Spacing when run from Netbeans

如果我通过在资源管理器窗口中双击它来运行SAME jar文件,那么Print就像这样。请注意,文本/字母的间距要宽得多。

Wide Text Spacing for the same JAR

知道发生了什么导致这种差异吗? Netbeans是否有一些导致此问题的默认属性?

1 个答案:

答案 0 :(得分:1)

NetBeans可能在不同于资源管理器的JVM上运行应用程序,或者可能正在更改其某些属性(-DXXX选项)。

尝试将RenderingHints.KEY_FRACTIONALMETRICS设为VALUE_FRACTIONALMETRICS_ON

(更多信息: http://docs.oracle.com/javase/7/docs/api/java/awt/RenderingHints.html#KEY_FRACTIONALMETRICS