0 Replies Latest reply on Jun 28, 2004 4:37 PM by 807587

    Printing in Mac OS X

    807587
      Hello all,

      I created a printable class that works in Windows and Linux, but for some reason will not work on Mac OS X. It either prints a blank page or throws a nasty exception:

      Exception in thread "Thread-0" java.lang.NullPointerException
      at javax.swing.plaf.metal.BumpBuffer.fillBumpBuffer(MetalBumps.java:183)
      at javax.swing.plaf.metal.BumpBuffer.<init>(MetalBumps.java:148)
      at javax.swing.plaf.metal.MetalBumps.getBuffer(MetalBumps.java:71)
      at javax.swing.plaf.metal.MetalBumps.paintIcon(MetalBumps.java:97)
      at javax.swing.plaf.metal.MetalScrollBarUI.paintThumb(MetalScrollBarUI.java:242)
      at javax.swing.plaf.basic.BasicScrollBarUI.paint(BasicScrollBarUI.java:301)
      at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
      at javax.swing.JComponent.paintComponent(JComponent.java:541)
      at javax.swing.JComponent.paint(JComponent.java:808)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at javax.swing.JLayeredPane.paint(JLayeredPane.java:557)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
      at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
      at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
      at java.awt.Container.paint(Container.java:1312)
      at apple.awt.CComponent.print(CComponent.java:130)
      at apple.awt.ContainerModel.print(ContainerModel.java:633)
      at java.awt.GraphicsCallback$PeerPrintCallback.run(GraphicsCallback.java:85)
      at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
      at java.awt.Component.printAll(Component.java:2532)
      at PrintUtilities.print(PrintUtilities.java:111)
      at apple.awt.CPrinterJob.printToPathGraphics(CPrinterJob.java:509)
      JavaAWT: Assertion failure: Java exception thrown
      JavaAWT: File src/macosx/native/apple/awt/util/AWTException.m; Line 40
      JavaAWT: Assertion failure: _javaException
      JavaAWT: File src/macosx/native/apple/awt/util/AWTException.m; Line 48
      2004-06-28 10:34:43.295 java[448] PMSessionEndDocumentNoDialog failed (error code = -30879)
      2004-06-28 10:34:43.297 java[448] *** -[NSAutoreleasePool dealloc]: Exception ignored while releasing an object in an autorelease pool: NSInternalInconsistencyException Failed to end PMPrintContext
      *** malloc[448]: Deallocation of a pointer not malloced: 0xbfffc7c0; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug



      I am using Java 1.4.2. Any ideas?

      Here is my code:
      import java.awt.*;
      import javax.swing.*;
      import java.awt.print.*;
      
      public class PrintUtilities implements Printable {
        private Component componentToBePrinted;
      
        public static void printComponent(Component c) {
          new PrintUtilities(c).print();
        }
      
        public PrintUtilities(Component componentToBePrinted) {
          this.componentToBePrinted = componentToBePrinted;
      
        }
      
        public void print() {
          PrinterJob printJob = PrinterJob.getPrinterJob();
      
              PageFormat pageFormat = new PageFormat();
              printJob.setPrintable(this, pageFormat);
             if(printJob.pageDialog(printJob.defaultPage()).getOrientation()==pageFormat.LANDSCAPE){
                try{
                        pageFormat.setOrientation(pageFormat.LANDSCAPE);
                    }
                catch(java.lang.IllegalStateException ise){
                        System.out.println("Print error...\n"+ise);
                   }
             }
             printJob.setPrintable(this,pageFormat);
      
             if (printJob.printDialog()){
                 try{
                      printJob.print();
                 }
                 catch(java.awt.print.PrinterException pe){
                      System.out.println("Could not print...\n" + pe);
                 }
             }
      
        }
      
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
      
           if (pageIndex > 0) {
           return(NO_SUCH_PAGE);
           }
           else {
      
                Graphics2D g2d = (Graphics2D)g;
      
                Toolkit toolkit = componentToBePrinted.getToolkit();
      
                /* scaling is added to fit printout of maximized birdseye
                window onto a 8.5 X 11 sheet of paper
                */
                Dimension screenSize = toolkit.getScreenSize();
      
                /** It is important that translation be done before the scaling.
                * Failure to follow this order will result in unusual behavior
                * such as strange cropping
                */
                g2d.translate(pageFormat.getImageableX(),
                pageFormat.getImageableY());
                g2d.scale(.45,.45);
                //g2d.scale(scaleX,scaleY);
      
                // speeds up printing process
                disableDoubleBuffering(componentToBePrinted);
      
                componentToBePrinted.printAll(g2d);
                enableDoubleBuffering(componentToBePrinted);
                return(PAGE_EXISTS);
           }
        }
      
        public static void disableDoubleBuffering(Component c) {
          RepaintManager currentManager = RepaintManager.currentManager(c);
          currentManager.setDoubleBufferingEnabled(false);
        }
      
        public static void enableDoubleBuffering(Component c) {
          RepaintManager currentManager = RepaintManager.currentManager(c);
          currentManager.setDoubleBufferingEnabled(true);
        }
      }
      Thanks