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