@@ -205,9 +205,14 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
205205 {
206206 return NO_SUCH_PAGE ;
207207 }
208+
209+ Graphics2D printerGraphics = null ;
210+ Graphics2D graphics2D = null ;
211+
208212 try
209213 {
210- Graphics2D graphics2D = (Graphics2D )graphics ;
214+ printerGraphics = (Graphics2D ) graphics ;
215+ graphics2D = printerGraphics ;
211216
212217 // capture the DPI that will be used for rasterizing the image
213218 // if rasterizing is specified
@@ -266,7 +271,6 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
266271 }
267272
268273 // rasterize to bitmap (optional)
269- Graphics2D printerGraphics = null ;
270274 BufferedImage image = null ;
271275 if (rasterDpi > 0 )
272276 {
@@ -276,7 +280,6 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
276280 (int )(imageableHeight * dpiScale / scale ),
277281 BufferedImage .TYPE_INT_ARGB );
278282
279- printerGraphics = graphics2D ;
280283 graphics2D = image .createGraphics ();
281284
282285 // rescale
@@ -303,12 +306,11 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
303306 }
304307
305308 // draw rasterized bitmap (optional)
306- if (printerGraphics != null )
309+ if (graphics2D != printerGraphics )
307310 {
308311 printerGraphics .setBackground (Color .WHITE );
309312 printerGraphics .clearRect (0 , 0 , image .getWidth (), image .getHeight ());
310313 printerGraphics .drawImage (image , 0 , 0 , null );
311- graphics2D .dispose ();
312314 }
313315
314316 return PAGE_EXISTS ;
@@ -317,6 +319,13 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
317319 {
318320 throw new PrinterIOException (e );
319321 }
322+ finally
323+ {
324+ if (graphics2D != null && graphics2D != printerGraphics )
325+ {
326+ graphics2D .dispose ();
327+ }
328+ }
320329 }
321330
322331 /**
0 commit comments