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