Skip to content

Commit 52a923f

Browse files
committed
8367702: PrintJob.getGraphics() should return null after PrintJob.end
Reviewed-by: azvegint
1 parent f489598 commit 52a923f

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/java.desktop/share/classes/sun/print/PrintJob2D.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ public Graphics getGraphics() {
7979
* needs to implement PrintGraphics, so we wrap
8080
* the Graphics2D instance.
8181
*/
82-
return new ProxyPrintGraphics(printJobDelegate.getGraphics(), this);
82+
Graphics g = printJobDelegate.getGraphics();
83+
if (g == null) { // PrintJob.end() has been called.
84+
return null;
85+
} else {
86+
return new ProxyPrintGraphics(g, this);
87+
}
8388
}
8489

8590
/**
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2004, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 50510568367702
27+
@key headful printer
28+
@summary PrintJob.getGraphics() should return null after PrintJob.end() is called.
29+
@run main GetGraphicsTest
30+
*/
31+
32+
import java.awt.Frame;
33+
import java.awt.Graphics;
34+
import java.awt.JobAttributes;
35+
import java.awt.PageAttributes;
36+
import java.awt.PrintJob;
37+
import java.awt.Toolkit;
38+
39+
public class GetGraphicsTest {
40+
41+
public static void main(String[] args) {
42+
43+
JobAttributes ja = new JobAttributes();
44+
ja.setDialog(JobAttributes.DialogType.NONE);
45+
PageAttributes pa = new PageAttributes();
46+
pa.setOrigin( PageAttributes.OriginType.PRINTABLE);
47+
48+
Toolkit tk = Toolkit.getDefaultToolkit();
49+
PrintJob pjob = tk.getPrintJob(new Frame(),"Printing Test", ja,pa);
50+
if (pjob != null) {
51+
pjob.end();
52+
Graphics pg = pjob.getGraphics();
53+
if (pg == null) {
54+
System.out.println("Graphics is null, TEST PASSES");
55+
}
56+
else {
57+
throw new RuntimeException("Graphics is NOT null, TEST FAILED");
58+
}
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)