1 /* 2 * Copyright (c) 2016, 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 * @test 25 * @bug 8162796 26 * @summary Verifies if LinearGradientPaint is printed in osx 27 * @requires (os.family == "mac") 28 * @run main/manual LinearGradientPrintingTest 29 */ 30 import java.awt.BorderLayout; 31 import java.awt.Color; 32 import java.awt.Component; 33 import java.awt.Container; 34 import java.awt.Dimension; 35 import java.awt.FlowLayout; 36 import java.awt.Graphics; 37 import java.awt.Graphics2D; 38 import java.awt.LinearGradientPaint; 39 import java.awt.Shape; 40 import java.awt.event.ActionEvent; 41 import java.awt.event.WindowAdapter; 42 import java.awt.event.WindowEvent; 43 import java.awt.geom.Point2D; 44 import java.awt.geom.Rectangle2D; 45 import java.awt.print.PageFormat; 46 import java.awt.print.Printable; 47 import java.awt.print.PrinterException; 48 import java.awt.print.PrinterJob; 49 import javax.swing.AbstractAction; 50 import javax.swing.JButton; 51 import javax.swing.JDialog; 52 import javax.swing.JFrame; 53 import javax.swing.JPanel; 54 import javax.swing.JTextArea; 55 import javax.swing.SwingUtilities; 56 import javax.swing.WindowConstants; 57 58 public class LinearGradientPrintingTest extends Component implements Printable { 59 private static Thread mainThread; 60 private static boolean testPassed; 61 private static boolean testGeneratedInterrupt; 62 private static JFrame f = null; 63 64 public static void main(String[] args) { 65 SwingUtilities.invokeLater(new Runnable() { 66 @Override 67 public void run() { 68 //createUI(); 69 doTest(LinearGradientPrintingTest::createUI); 70 } 71 }); 72 mainThread = Thread.currentThread(); 73 try { 74 Thread.sleep(120000); 75 } catch (InterruptedException e) { 76 if (!testPassed && testGeneratedInterrupt) { 77 throw new RuntimeException("LinearGradientPaint did not print"); 78 } 79 } 80 if (!testGeneratedInterrupt) { 81 throw new RuntimeException("user has not executed the test"); 82 } 83 } 84 85 public static void createUI() { 86 f = new JFrame("LinearGradient Printing Test"); 87 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 88 final LinearGradientPrintingTest gpt = new LinearGradientPrintingTest(); 89 Container c = f.getContentPane(); 90 c.add(BorderLayout.CENTER, gpt); 91 92 final JButton print = new JButton("Print"); 93 print.addActionListener(new AbstractAction() { 94 @Override 95 public void actionPerformed(ActionEvent e) { 96 PrinterJob job = PrinterJob.getPrinterJob(); 97 job.setPrintable(gpt); 98 final boolean doPrint = job.printDialog(); 99 if (doPrint) { 100 try { 101 job.print(); 102 } catch (PrinterException ex) { 103 throw new RuntimeException(ex); 104 } 105 } 106 } 107 }); 108 c.add(print, BorderLayout.SOUTH); 109 110 f.pack(); 111 f.setVisible(true); 112 } 113 114 public Dimension getPreferredSize() { 115 return new Dimension(500,500); 116 } 117 118 public void paint(Graphics g) { 119 doPaint((Graphics2D)g); 120 } 121 122 public int print( Graphics graphics, PageFormat format, int index ) { 123 Graphics2D g2d = (Graphics2D)graphics; 124 g2d.translate(format.getImageableX(), format.getImageableY()); 125 doPaint(g2d); 126 return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE; 127 } 128 129 static final float DIM = 100; 130 public void doPaint(Graphics2D g2d) { 131 132 g2d.translate(DIM*0.2, DIM*0.2); 133 Shape s = new Rectangle2D.Float(0, 0, DIM*2, DIM*2); 134 Point2D.Double p1 = new Point2D.Double(0.0, 0.0); 135 Point2D.Double p2 = new Point2D.Double(DIM, DIM); 136 137 // LinearGradientPaint 138 //g2d.translate(DIM*2.2, 0); 139 Color colors[] = { Color.red, Color.blue} ; 140 float fractions[] = { 0.0f, 1.0f }; 141 142 LinearGradientPaint lgp = 143 new LinearGradientPaint(p1, p2, fractions, colors, 144 LinearGradientPaint.CycleMethod.NO_CYCLE); 145 g2d.setPaint(lgp); 146 g2d.fill(s); 147 148 g2d.translate(DIM*2.2, 0); 149 Color colors1[] = { Color.red, Color.blue, Color.green, Color.white} ; 150 float fractions1[] = { 0.0f, 0.3f, 0.6f, 1.0f }; 151 152 LinearGradientPaint lgp1 = 153 new LinearGradientPaint(p1, p2, fractions1, colors1, 154 LinearGradientPaint.CycleMethod.REFLECT); 155 g2d.setPaint(lgp1); 156 g2d.fill(s); 157 158 g2d.translate(-DIM*2.2, DIM*2.2); 159 Color colors2[] = { Color.red, Color.blue, Color.green, Color.white} ; 160 float fractions2[] = { 0.0f, 0.3f, 0.6f, 1.0f }; 161 162 LinearGradientPaint lgp2 = 163 new LinearGradientPaint(p1, p2, fractions2, colors2, 164 LinearGradientPaint.CycleMethod.REPEAT); 165 g2d.setPaint(lgp2); 166 g2d.fill(s); 167 } 168 169 public static synchronized void pass() { 170 testPassed = true; 171 testGeneratedInterrupt = true; 172 mainThread.interrupt(); 173 } 174 175 public static synchronized void fail() { 176 testPassed = false; 177 testGeneratedInterrupt = true; 178 mainThread.interrupt(); 179 } 180 181 private static void doTest(Runnable action) { 182 String description 183 = " A LinearGradientPaint graphics will be shown on console.\n" 184 + " The same graphics is sent to printer.\n" 185 + " Please verify if LinearGradientPaint shading is printed.\n" 186 + " If none is printed, press FAIL else press PASS"; 187 188 final JDialog dialog = new JDialog(); 189 dialog.setTitle("printSelectionTest"); 190 JTextArea textArea = new JTextArea(description); 191 textArea.setEditable(false); 192 final JButton testButton = new JButton("Start Test"); 193 final JButton passButton = new JButton("PASS"); 194 passButton.setEnabled(false); 195 passButton.addActionListener((e) -> { 196 f.dispose(); 197 dialog.dispose(); 198 pass(); 199 }); 200 final JButton failButton = new JButton("FAIL"); 201 failButton.setEnabled(false); 202 failButton.addActionListener((e) -> { 203 f.dispose(); 204 dialog.dispose(); 205 fail(); 206 }); 207 testButton.addActionListener((e) -> { 208 testButton.setEnabled(false); 209 action.run(); 210 passButton.setEnabled(true); 211 failButton.setEnabled(true); 212 }); 213 JPanel mainPanel = new JPanel(new BorderLayout()); 214 mainPanel.add(textArea, BorderLayout.CENTER); 215 JPanel buttonPanel = new JPanel(new FlowLayout()); 216 buttonPanel.add(testButton); 217 buttonPanel.add(passButton); 218 buttonPanel.add(failButton); 219 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 220 dialog.add(mainPanel); 221 222 dialog.pack(); 223 dialog.setVisible(true); 224 dialog.addWindowListener(new WindowAdapter() { 225 @Override 226 public void windowClosing(WindowEvent e) { 227 System.out.println("main dialog closing"); 228 testGeneratedInterrupt = false; 229 mainThread.interrupt(); 230 } 231 }); 232 } 233 234 }