Is Printing broken in Java 7.

Abhinay Reddyreddy Abhinay.Reddyreddy at mathworks.com
Mon Feb 25 11:07:13 PST 2013


Hi,

The following code works fine on my mac with Java 6 but it is broken with Java 7.

import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.DialogTypeSelection;
//import sun.print.DialogTypeSelection;
import javax.swing.*;

public class TextAreaPrintingDemo extends javax.swing.JFrame {

    public TextAreaPrintingDemo() {
    }
    private static class TextPrintable implements Printable {

        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if(pageIndex > 1) {
                return NO_SUCH_PAGE;
            } else {
                Graphics2D g2d = (Graphics2D)graphics;
                g2d.setColor(Color.GREEN);
                g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                g2d.setFont(new Font("Monaco", Font.ITALIC, 13));
                g2d.drawString("This text is being printed using JRE version: " + System.getProperty("java.version") , 30, 30);
                g2d.drawString("with java home: "+ System.getProperty("java.home") , 30, 70);
                return PAGE_EXISTS;
            }
        }
    }

    public static void main(String args[]) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                PrinterJob job = PrinterJob.getPrinterJob();
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(DialogTypeSelection.NATIVE);
                job.printDialog(pras);
                TextPrintable myPrintable = new TextPrintable();
                job.setPrintable(myPrintable);
                try {
                    job.print(pras);
                } catch(Exception e)
                {
                    System.out.println("exception occured");
                }

            }
        });
    }

}

It looks like with Java 7, drawString function ignores font and color while rendering the text. I have attached the outputs for reference.

Could anyone let me know if this is a known issue. If it is known, is there a bug reported and any suggested workaround ?





Thanks,
Abhinay.


More information about the macosx-port-dev mailing list