1 /*
2  * Copyright (c) 2020, 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 8240654
27  * @summary Test printing alpha colors - banded printing works with ZGC.
28  * @key headful printer
29  * @requires (os.family == "windows")
30  * @requires vm.gc.Z
31  * @run main/manual/othervm -XX:+UseZGC -Dsun.java2d.d3d=false AlphaPrintTest
32  */
33 
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Frame;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.GridLayout;
40 
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 
44 import java.awt.print.PageFormat;
45 import java.awt.print.Printable;
46 import java.awt.print.PrinterException;
47 import java.awt.print.PrinterJob;
48 
49 import javax.swing.JButton;
50 import javax.swing.JFrame;
51 import javax.swing.JPanel;
52 import javax.swing.JTextArea;
53 import javax.swing.SwingUtilities;
54 import javax.swing.WindowConstants;
55 
56 public class AlphaPrintTest extends JPanel implements Printable {
57 
58     static final int W=400, H=600;
59 
60     static volatile JFrame frame = null;
61     static volatile AlphaPrintTest comp = null;
62     static Color color = Color.red;
63     static volatile boolean passed = false;
64     static volatile boolean printInvoked = false;
65     static volatile boolean done = false;
66 
main(String[] args)67     public static void main(String[] args) throws Exception {
68 
69         SwingUtilities.invokeAndWait(() -> {
70             frame = new JFrame("Alpha Color Print Test");
71             frame.setLayout(new GridLayout(1, 2));
72             frame.add(comp = new AlphaPrintTest());
73             frame.add(new InstructionPanel());
74             frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
75             frame.pack();
76             frame.setVisible(true);
77         });
78 
79         while (!done || !printInvoked) {
80             Thread.sleep(1000);
81         }
82 
83         SwingUtilities.invokeAndWait(() -> frame.dispose());
84 
85         if (!passed) {
86             throw new RuntimeException("Test failed.");
87         }
88     }
89 
90     @Override
getPreferredSize()91     public Dimension getPreferredSize() {
92         return new Dimension(W, H);
93     }
94 
95 
96     @Override
getMinimumSize()97     public Dimension getMinimumSize() {
98         return getPreferredSize();
99     }
100 
101     @Override
paintComponent(Graphics g)102     protected void paintComponent(Graphics g) {
103         super.paintComponent(g);
104         paintContent(g);
105     };
106 
paintContent(Graphics g)107     private void paintContent(Graphics g) {
108         Color c = new Color(255, 0, 0, 240); // not a solid color.
109         g.setColor(c);
110         g.drawLine(0, 0, W, H);
111         g.drawLine(W, 0, 0, H);
112 
113         for (int i=10; i < 150; i+=10) {
114             g.drawRect(i, i, W-(i*2), H-(i*2));
115         }
116         g.drawString("Alpha Paint Test", W/2-30, H/2);
117     }
118 
print(Graphics g, PageFormat pf, int pageIndex)119     public int print(Graphics g, PageFormat pf, int pageIndex) {
120         if (pageIndex == 0) {
121             Graphics2D g2d = (Graphics2D)g;
122             g2d.translate(pf.getImageableX(), pf.getImageableY());
123             paintContent(g);
124             return Printable.PAGE_EXISTS;
125         }
126         return Printable.NO_SUCH_PAGE;
127     }
128 
doPrint()129     public void doPrint() {
130         printInvoked = true;
131         PrinterJob pj = PrinterJob.getPrinterJob();
132         pj.setPrintable(this);
133         if (pj.printDialog()) {
134             try {
135                 pj.print();
136             } catch (PrinterException e) {
137                 e.printStackTrace();
138                 done = true;
139             }
140         }
141     }
142 
doClose(boolean pass)143     public void doClose(boolean pass) {
144         if (printInvoked) {
145             passed = pass;
146             done = true;
147         }
148     }
149 }
150 
151 class  InstructionPanel extends JPanel implements ActionListener {
152 
153     static final String INSTRUCTIONS =
154             "You must have a printer to peform this test.\n" +
155                     "Press the print button which will bring up a print dialog." +
156                     "Select a suitable printer, and confirm to print. " +
157                     "Examine the printed output. It should closely resemble the rendering in" +
158                     " the panel to the left. If yes, press PASS, else press FAIL";
159 
InstructionPanel()160     InstructionPanel() {
161         GridLayout gl1 = new GridLayout(2, 1);
162         setLayout(gl1);
163         JTextArea ta = new JTextArea(INSTRUCTIONS);
164         ta.setEditable(false);
165         ta.setLineWrap(true);
166         ta.setWrapStyleWord(true);
167         add(ta);
168         JPanel p = new JPanel();
169         JButton print = new JButton("Print");
170         JButton pass = new JButton("PASS");
171         JButton fail = new JButton("FAIL");
172         print.addActionListener(this);
173         pass.addActionListener(this);
174         fail.addActionListener(this);
175         p.add(print);
176         p.add(pass);
177         p.add(fail);
178         add(p);
179     }
180     @Override
getPreferredSize()181     public Dimension getPreferredSize() {
182         return new Dimension(200, 600);
183     }
184 
185 
186     @Override
getMinimumSize()187     public Dimension getMinimumSize() {
188         return getPreferredSize();
189     }
actionPerformed(ActionEvent e)190     public void actionPerformed(ActionEvent e) {
191         String cmd = e.getActionCommand();
192         switch (cmd) {
193             case "Print" -> AlphaPrintTest.comp.doPrint();
194             case "PASS" -> AlphaPrintTest.comp.doClose(true);
195             case "FAIL" -> AlphaPrintTest.comp.doClose(false);
196         }
197 
198     }
199 
200 }
201