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 8040635 26 * @summary Verifies if TexturePaint is printed in osx 27 * @run main/manual TexturePaintPrintingTest 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.TexturePaint; 38 import java.awt.event.ActionEvent; 39 import java.awt.event.WindowAdapter; 40 import java.awt.event.WindowEvent; 41 import java.awt.geom.Rectangle2D; 42 import java.awt.image.BufferedImage; 43 import java.awt.print.PageFormat; 44 import java.awt.print.Printable; 45 import static java.awt.print.Printable.NO_SUCH_PAGE; 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 TexturePaintPrintingTest extends Component implements Printable { printTexture()58 private static void printTexture() { 59 f = new JFrame("Texture Printing Test"); 60 f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 61 final TexturePaintPrintingTest gpt = new TexturePaintPrintingTest(); 62 Container c = f.getContentPane(); 63 c.add(BorderLayout.CENTER, gpt); 64 65 final JButton print = new JButton("Print"); 66 print.addActionListener(new AbstractAction() { 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 PrinterJob job = PrinterJob.getPrinterJob(); 70 job.setPrintable(gpt); 71 final boolean doPrint = job.printDialog(); 72 if (doPrint) { 73 try { 74 job.print(); 75 } catch (PrinterException ex) { 76 throw new RuntimeException(ex); 77 } 78 } 79 } 80 }); 81 c.add(print, BorderLayout.SOUTH); 82 83 f.pack(); 84 f.setVisible(true); 85 } 86 getPreferredSize()87 public Dimension getPreferredSize() { 88 return new Dimension(500,500); 89 } 90 paint(Graphics g)91 public void paint(Graphics g) { 92 doPaint((Graphics2D)g); 93 } 94 print( Graphics graphics, PageFormat format, int index )95 public int print( Graphics graphics, PageFormat format, int index ) { 96 Graphics2D g2d = (Graphics2D)graphics; 97 g2d.translate(format.getImageableX(), format.getImageableY()); 98 doPaint(g2d); 99 return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE; 100 } 101 102 static final float DIM = 100; doPaint(Graphics2D g2d)103 public void doPaint(Graphics2D g2d) { 104 BufferedImage patternImage = new BufferedImage(2,2,BufferedImage.TYPE_INT_ARGB); 105 Graphics gImage = patternImage.getGraphics(); 106 gImage.setColor(Color.WHITE); 107 gImage.drawLine(0,1,1,0); 108 gImage.setColor(Color.BLACK); 109 gImage.drawLine(0,0,1,1); 110 gImage.dispose(); 111 112 Rectangle2D.Double shape = new Rectangle2D.Double(0,0,DIM*6/5, DIM*8/5); 113 g2d.setPaint(new TexturePaint(patternImage, new Rectangle2D.Double(0,0, 114 DIM*6/50, DIM*8/50))); 115 g2d.fill(shape); 116 g2d.setPaint(Color.BLACK); 117 g2d.draw(shape); 118 119 } 120 pass()121 public static synchronized void pass() { 122 testPassed = true; 123 testGeneratedInterrupt = true; 124 mainThread.interrupt(); 125 } 126 fail()127 public static synchronized void fail() { 128 testPassed = false; 129 testGeneratedInterrupt = true; 130 mainThread.interrupt(); 131 } 132 133 private static Thread mainThread; 134 private static boolean testPassed; 135 private static boolean testGeneratedInterrupt; 136 private static JFrame f = null; 137 main(String[] args)138 public static void main(String[] args) { 139 SwingUtilities.invokeLater(new Runnable() { 140 @Override 141 public void run() { 142 //createUI(); 143 doTest(TexturePaintPrintingTest::printTexture); 144 } 145 }); 146 mainThread = Thread.currentThread(); 147 try { 148 Thread.sleep(120000); 149 } catch (InterruptedException e) { 150 if (!testPassed && testGeneratedInterrupt) { 151 throw new RuntimeException("TexturePaint did not print"); 152 } 153 } 154 if (!testGeneratedInterrupt) { 155 throw new RuntimeException("user has not executed the test"); 156 } 157 } 158 doTest(Runnable action)159 private static void doTest(Runnable action) { 160 String description 161 = " A TexturePaint graphics will be shown on console.\n" 162 + " The same graphics is sent to printer.\n" 163 + " Please verify if TexturePaint shading is printed.\n" 164 + " If none is printed, press FAIL else press PASS"; 165 166 final JDialog dialog = new JDialog(); 167 dialog.setTitle("printSelectionTest"); 168 JTextArea textArea = new JTextArea(description); 169 textArea.setEditable(false); 170 final JButton testButton = new JButton("Start Test"); 171 final JButton passButton = new JButton("PASS"); 172 passButton.setEnabled(false); 173 passButton.addActionListener((e) -> { 174 f.dispose(); 175 dialog.dispose(); 176 pass(); 177 }); 178 final JButton failButton = new JButton("FAIL"); 179 failButton.setEnabled(false); 180 failButton.addActionListener((e) -> { 181 f.dispose(); 182 dialog.dispose(); 183 fail(); 184 }); 185 testButton.addActionListener((e) -> { 186 testButton.setEnabled(false); 187 action.run(); 188 passButton.setEnabled(true); 189 failButton.setEnabled(true); 190 }); 191 JPanel mainPanel = new JPanel(new BorderLayout()); 192 mainPanel.add(textArea, BorderLayout.CENTER); 193 JPanel buttonPanel = new JPanel(new FlowLayout()); 194 buttonPanel.add(testButton); 195 buttonPanel.add(passButton); 196 buttonPanel.add(failButton); 197 mainPanel.add(buttonPanel, BorderLayout.SOUTH); 198 dialog.add(mainPanel); 199 200 dialog.pack(); 201 dialog.setVisible(true); 202 dialog.addWindowListener(new WindowAdapter() { 203 @Override 204 public void windowClosing(WindowEvent e) { 205 System.out.println("main dialog closing"); 206 testGeneratedInterrupt = false; 207 mainThread.interrupt(); 208 } 209 }); 210 } 211 } 212