JDK-4212564 : Component.printAll() passes invalid Graphics object
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.5,1.1.6,1.1.7,1.2.0,1.2.1
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-02-18
  • Updated: 1999-04-27
  • Resolved: 1999-03-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.2.2 1.2.2Fixed
Related Reports
Duplicate :  
Duplicate :  
Description

Name: clC74495			Date: 02/18/99


Component.printAll() takes a Graphics object, creates a new one,
and calls either lightweightPrint() or peer.print().  Problem
is, printAll() passes the original Graphics object, not the new
one...   [ ALSO HAPPENS IN JDK1.2 ]

  From Component.java...

  public void printAll(Graphics g) {
    ComponentPeer peer = this.peer;
    if (visible && (peer != null)) {
      validate();
      Graphics cg = g.create(0, 0, width, height);
      cg.setFont(getFont());
      try {
        if (peer instanceof java.awt.peer.LightweightPeer) {
          lightweightPrint(g); /*** ERROR ***/
        }
        else {
          peer.print(g); /*** ERROR ***/
        }
      } finally {
        cg.dispose();
      }
    }
  }

This is a problem because while 'cg' contains a valid clipRect,
'g' does not.  Lightweight code calling g.getClipRect() will get
a null Rectangle unless the correct Graphics object is passed
down the chain.
(Review ID: 54316)
======================================================================

Name: skT88420			Date: 05/20/99


Trying to print JFrame derived class,
printAll() throws NullPointerException.

Using JDK1.1.6, Swing 1.03, Netscape 4.5X/4.6
[which JavaVM supports 1.1.5) on NT4sp3.



java.lang.NullPointerException

  at java.awt.Rectangle.intersects(Compiled Code)

  at java.awt.Container.print(Compiled Code)

  at sun.awt.windows.WCanvasPeer.print(Compiled Code)

  at sun.awt.windows.WPanelPeer.print(Compiled Code)

  at java.awt.Component.printAll(Compiled Code)

  at VyGuiCommon.FormMgr.printForm(Compiled Code)

  at VyGuiCommon.ActionEventHandler.actionPerformed(Compiled Code)

  at com.sun.java.swing.AbstractButton.fireActionPerformed(Compiled Code)

  at com.sun.java.swing.AbstractButton$ForwardActionEvents.actionPerformed(Compiled Code)

  at com.sun.java.swing.DefaultButtonModel.fireActionPerformed(Compiled Code)

  at com.sun.java.swing.DefaultButtonModel.setPressed(Compiled Code)

  at com.sun.java.swing.plaf.basic.BasicButtonListener.mouseReleased(Compiled Code)

  at java.awt.Component.processMouseEvent(Compiled Code)

  at java.awt.Component.processEvent(Compiled Code)

  at java.awt.Container.processEvent(Compiled Code)

  at java.awt.Component.dispatchEventImpl(Compiled Code)

  at java.awt.Container.dispatchEventImpl(Compiled Code)

  at java.awt.Component.dispatchEvent(Compiled Code)

  at java.awt.LightweightDispatcher.retargetMouseEvent(Compiled Code)

  at java.awt.LightweightDispatcher.processMouseEvent(Compiled Code)

  at java.awt.LightweightDispatcher.dispatchEvent(Compiled Code)

  at java.awt.Container.dispatchEventImpl(Compiled Code)

  at java.awt.Window.dispatchEventImpl(Compiled Code)

* at java.awt.Component.dispatchEvent(Compiled Code)

  at java.awt.EventDispatchThread$EventPump.dispatchEvents(Compiled Code)

  at java.awt.EventDispatchThread.run(Compiled Code)

  at netscape.applet.DerivedAppletFrame$AppletEventDispatchThread.run(Compiled Code)

   Deactivate:java.awt.event.WindowEvent[WINDOW_DEACTIVATED] on frame6

   Leaving fieldVyGuiCommon.VyButton[Print,342,4,41x63,layout=com.sun.java.swing.OverlayLayout]



Source code below:

public void printForm()
    {        
        try { 
            System.out.println("** Install NETSCAPE SEC PrintJob Privilege ***"); 
            netscape.security.PrivilegeManager.enablePrivilege("UniversalPrintJobAccess");
            System.out.println("Enabled PrintJob");
        } catch (Exception ex) { 
            System.out.println("printForm Ex:"+ex);           
            return;
        }
        if (frame == null) 
        {
            VyTrace.print(2,"Frame not set");
            return;
        }

        if (_applet == null)
        {
            //VyTrace.print(2,"Applet not set");
            return;
        }

        PrintJob printJob = null;
        printJob = _applet.getToolkit().getPrintJob(frame,"Print Screen",null);        
        if (printJob != null) {
            //vyTrace.print(2,"get Screen");
            Graphics screen = printJob.getGraphics();
            if (screen != null) {
                //VyTrace.print(1,"Print screen");  
                 
                try {                    
                   frame.printAll(screen);

                   System.out.println("PrintAll done");
                } catch (Exception ex) 
                   { System.out.println("Exception: "+ex); } 
                
                screen.dispose();
               
            }
            printJob.end();            
        }
        else {           
            System.out.println("printJob is null ********** Cannot print. Make sure a printer is defined");
        }

    }
(Review ID: 83310)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.2.2 FIXED IN: 1.2.2 INTEGRATED IN: 1.2.2
14-06-2004

WORK AROUND Name: clC74495 Date: 02/18/99 The workaround is to correctly pass 'cg' instead of 'g'... public void printAll(Graphics g) { ComponentPeer peer = this.peer; if (visible && (peer != null)) { validate(); Graphics cg = g.create(0, 0, width, height); cg.setFont(getFont()); try { if (peer instanceof java.awt.peer.LightweightPeer) { lightweightPrint(cg); /*** CORRECT ***/ } else { peer.print(cg); /*** CORRECT ***/ } } finally { cg.dispose(); } } } I've verified this problem will occur in 1.1.7B, but I haven't checked 1.1.8 or 1.2. I've also only verified this problem on Windows NT. Making this change to our Component.java eliminates the resulting NullPointerException and allows the print to succeed. Thanks! ======================================================================
11-06-2004

EVALUATION The suggested fix is correct. david.mendenhall@eng 1999-02-22
22-02-1999