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