Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drawing a JComponent to a graphics object

0 views
Skip to first unread message

Paul Keeble

unread,
Oct 11, 2001, 2:42:32 PM10/11/01
to
What I am attempting to do is to print a copy of some JComponent(In this case a
JPanel with various other components in it) to the printer. I've worked out how
to send a java.awt.print.Printable object to the printer but alas I can't get
the paintAll(Graphics g) to do anything properly at all.

If I have a custom JComponent that implements the Prinatable interface and I
basically put in the print method the following:-

public int print(Graphics g,PageFormat format,int pageIndex)
{
//I have to define transformations so that co-ordinates are correct here,
already got this code working fine.

paintAll(g);

if(pageIndex>0)
return Printable.NO_SUCH_PAGE;

return Printable.PAGE_EXISTS;
}

There are 2 possible states a JComponent that might be printed could be in (At
least I've tried both).
1) Its been constructed but not put into the actual GUI yet.....it has no size
and for obvious reasons I seen nothing but a blank page.
2) The JComponent has been constructed and placed into the GUI, it can be seen
on screen and is passed to be printed. In this case I get the following
exception thrown during the attempt to print:-

java.lang.NullPointerException
at sun.awt.windows.WPathGraphics.hasTransparentPixels(WPathGraphics.java
:1332)
at sun.awt.windows.WPathGraphics.drawImageToGDI(WPathGraphics.java:1168)

at sun.awt.windows.WPathGraphics.drawImage(WPathGraphics.java:753)
at sun.awt.windows.WPathGraphics.drawImage(WPathGraphics.java:668)
at sun.awt.windows.WPathGraphics.drawImage(WPathGraphics.java:607)
at sun.awt.windows.WPathGraphics.drawImage(WPathGraphics.java:550)
at sun.awt.windows.WPathGraphics.drawImage(WPathGraphics.java:459)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4754)

at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4697)
at javax.swing.JComponent.paint(JComponent.java:788)
at java.awt.Component.lightweightPaint(Component.java:2341)
at java.awt.Container.lightweightPaint(Container.java:1207)
at java.awt.GraphicsCallback$PeerPaintCallback.run(GraphicsCallback.java
:70)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:
63)
at java.awt.Component.paintAll(Component.java:2326)
at screen2.client.gui.DatabaseObjectJPanel.print(DatabaseObjectJPanel.ja
va:311)
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1494)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1065)
at sun.print.Win32PrintJob.printableJob(Win32PrintJob.java:440)
at sun.print.Win32PrintJob.print(Win32PrintJob.java:340)

***Past this point of the trace its all my event Listener model stuff for the
print button.***

My question is thus, how do I get rid of this NullPointerException within Sun's
code, is there something I can set to say no there aren't any transparent pixels
or to avoid the check being taken?


0 new messages