Skip to content

Commit 57abd30

Browse files
committed
backport e4bb47bcbbfe55dee4825d00a5b5eb76125a6ef5
1 parent 837d504 commit 57abd30

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

src/java.desktop/share/classes/javax/swing/JViewport.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -603,15 +603,11 @@ public final Insets getInsets(Insets insets) {
603603

604604

605605
private Graphics getBackingStoreGraphics(Graphics g) {
606-
if (!SwingUtilities2.isPrinting(g)) {
607-
Graphics bsg = backingStoreImage.getGraphics();
608-
bsg.setColor(g.getColor());
609-
bsg.setFont(g.getFont());
610-
bsg.setClip(g.getClipBounds());
611-
return bsg;
612-
} else {
613-
return g;
614-
}
606+
Graphics bsg = backingStoreImage.getGraphics();
607+
bsg.setColor(g.getColor());
608+
bsg.setFont(g.getFont());
609+
bsg.setClip(g.getClipBounds());
610+
return bsg;
615611
}
616612

617613

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,6 +69,7 @@
6969
import java.awt.image.DataBufferInt;
7070
import java.awt.image.ImageObserver;
7171
import java.awt.image.IndexColorModel;
72+
import java.awt.image.MultiResolutionImage;
7273
import java.awt.image.Raster;
7374
import java.awt.image.RenderedImage;
7475
import java.awt.image.SampleModel;
@@ -1138,6 +1139,9 @@ protected BufferedImage getBufferedImage(Image img) {
11381139
// VI needs to make a new BI: this is unavoidable but
11391140
// I don't expect VI's to be "huge" in any case.
11401141
return ((VolatileImage)img).getSnapshot();
1142+
} else if (img instanceof MultiResolutionImage) {
1143+
return convertToBufferedImage((MultiResolutionImage) img,
1144+
img.getWidth(null), img.getHeight(null));
11411145
} else {
11421146
// may be null or may be some non-standard Image which
11431147
// shouldn't happen as Image is implemented by the platform
@@ -1148,6 +1152,18 @@ protected BufferedImage getBufferedImage(Image img) {
11481152
}
11491153
}
11501154

1155+
protected BufferedImage convertToBufferedImage(MultiResolutionImage multiResolutionImage,
1156+
double width, double height ) {
1157+
Image resolutionImage = multiResolutionImage.getResolutionVariant(width, height);
1158+
BufferedImage bufferedImage = new BufferedImage(resolutionImage.getWidth(null),
1159+
resolutionImage.getHeight(null),
1160+
BufferedImage.TYPE_INT_ARGB);
1161+
Graphics2D g2d = bufferedImage.createGraphics();
1162+
g2d.drawImage(resolutionImage, 0, 0, (int) width, (int) height, null);
1163+
g2d.dispose();
1164+
return bufferedImage;
1165+
}
1166+
11511167
/**
11521168
* Return true if the BufferedImage argument has non-opaque
11531169
* bits in it and therefore can not be directly rendered by

src/java.desktop/share/classes/sun/swing/SwingUtilities2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1317,7 +1317,7 @@ public int hashCode() {
13171317
* returns true if the Graphics is print Graphics
13181318
* false otherwise
13191319
*/
1320-
public static boolean isPrinting(Graphics g) {
1320+
static boolean isPrinting(Graphics g) {
13211321
return (g instanceof PrinterGraphics || g instanceof PrintGraphics);
13221322
}
13231323

test/jdk/javax/swing/JTable/JTableScrollPrintTest.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,53 +36,48 @@
3636
import javax.swing.JScrollPane;
3737
import javax.swing.JTable;
3838
import javax.swing.JViewport;
39-
import javax.swing.SwingUtilities;
4039
import javax.swing.table.DefaultTableModel;
4140

4241
/*
4342
* @test
4443
* @key headful
45-
* @bug 8210807
44+
* @bug 8210807 8322140
4645
* @library /java/awt/regtesthelpers
4746
* @build PassFailJFrame
4847
* @summary Test to check if JTable can be printed when JScrollPane added to it.
4948
* @run main/manual JTableScrollPrintTest
5049
*/
5150

5251
public class JTableScrollPrintTest {
53-
public static JFrame frame;
54-
public static PassFailJFrame passFailJFrame;
55-
5652
public static void main(String[] args) throws Exception {
57-
SwingUtilities.invokeAndWait(() -> {
58-
try {
59-
initialize();
60-
} catch (Exception e) {
61-
throw new RuntimeException(e);
62-
}
63-
});
64-
passFailJFrame.awaitAndCheck();
65-
}
66-
67-
public static void initialize() throws Exception {
68-
final String INSTRUCTIONS = """
53+
String INSTRUCTIONS = """
6954
Instructions to Test:
7055
1. Print table onto Paper/PDF, using the Print Dialog.
7156
2. If entire table is printed, then the Test is PASS.
7257
3. If table is partially printed without table cells,
7358
then the Test is FAIL.
7459
""";
75-
TestTable testTable = new TestTable(true);
76-
frame = new JFrame("JTable Print Test");
77-
passFailJFrame = new PassFailJFrame("Test Instructions", INSTRUCTIONS, 5L, 6, 35);
60+
PassFailJFrame.builder()
61+
.title("Test Instructions")
62+
.instructions(INSTRUCTIONS)
63+
.rows(6)
64+
.columns(35)
65+
.testUI(JTableScrollPrintTest::initialize)
66+
.build()
67+
.awaitAndCheck();
68+
}
7869

70+
public static JFrame initialize() {
71+
TestTable testTable = new TestTable(true);
72+
JFrame frame = new JFrame("JTable Print Test");
7973
PassFailJFrame.addTestWindow(frame);
8074
PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.VERTICAL);
8175
frame.add(testTable);
8276
frame.pack();
8377
frame.setVisible(true);
8478
PrintUtilities printerJob = new PrintUtilities(testTable);
8579
printerJob.print("Test BackingStore Image Print");
80+
return frame;
8681
}
8782

8883
public static class TestTable extends JPanel {
@@ -103,7 +98,7 @@ public TestTable(Boolean useScrollPane) {
10398

10499
JTable table = new JTable(model);
105100

106-
if (useScrollPane == true) {
101+
if (useScrollPane) {
107102
JScrollPane sp = new JScrollPane(table,
108103
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
109104
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
@@ -117,7 +112,7 @@ public TestTable(Boolean useScrollPane) {
117112
}
118113

119114
static class PrintUtilities implements Printable {
120-
private Component componentToBePrinted;
115+
private final Component componentToBePrinted;
121116

122117
public void printComponent(Component c, String jobname) {
123118
new PrintUtilities(c).print(jobname);

0 commit comments

Comments
 (0)