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  * @run main/manual RadialGradientPrintingTest
  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.RadialGradientPaint;
  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 static java.awt.print.Printable.NO_SUCH_PAGE;
  47 import static java.awt.print.Printable.PAGE_EXISTS;
  48 import java.awt.print.PrinterException;
  49 import java.awt.print.PrinterJob;
  50 import javax.swing.AbstractAction;
  51 import javax.swing.JButton;
  52 import javax.swing.JDialog;
  53 import javax.swing.JFrame;
  54 import javax.swing.JPanel;
  55 import javax.swing.JTextArea;
  56 import javax.swing.SwingUtilities;
  57 import javax.swing.WindowConstants;
  58 
  59 public class RadialGradientPrintingTest extends Component implements Printable {
  60     private static Thread mainThread;
  61     private static boolean testPassed;
  62     private static boolean testGeneratedInterrupt;
  63     private static JFrame f = null;
  64     
  65     public static void main(String[] args) {
  66         SwingUtilities.invokeLater(new Runnable() {
  67             @Override
  68             public void run() {
  69                 //createUI();
  70                 doTest(RadialGradientPrintingTest::createUI);
  71             }
  72         });
  73         mainThread = Thread.currentThread();
  74         try {
  75             Thread.sleep(120000);
  76         } catch (InterruptedException e) {
  77             if (!testPassed && testGeneratedInterrupt) {
  78                 throw new RuntimeException("LinearGradientPaint did not print");
  79             }
  80         }
  81         if (!testGeneratedInterrupt) {
  82             throw new RuntimeException("user has not executed the test");
  83         }
  84     }
  85 
  86     public static void createUI() {
  87         f = new JFrame("RadialGradient Printing Test");
  88         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  89         final RadialGradientPrintingTest gpt = new RadialGradientPrintingTest();
  90         Container c = f.getContentPane();
  91         c.add(BorderLayout.CENTER, gpt);
  92         
  93         final JButton print = new JButton("Print");
  94         print.addActionListener(new AbstractAction() {
  95             @Override
  96             public void actionPerformed(ActionEvent e) {
  97                 PrinterJob job = PrinterJob.getPrinterJob();
  98                 job.setPrintable(gpt);
  99                 final boolean doPrint = job.printDialog();
 100                 if (doPrint) {
 101                     try {
 102                         job.print();
 103                     } catch (PrinterException ex) {
 104                         throw new RuntimeException(ex);
 105                     }
 106                 }
 107             }
 108         });
 109         c.add(print, BorderLayout.SOUTH);
 110         
 111         f.pack();
 112         f.setVisible(true);
 113     }
 114 
 115     public Dimension getPreferredSize() {
 116         return new Dimension(500,500);
 117     }
 118 
 119     public void paint(Graphics g) {
 120         doPaint((Graphics2D)g);
 121     }
 122 
 123     public int print( Graphics graphics, PageFormat format, int index ) {
 124         Graphics2D g2d = (Graphics2D)graphics;
 125         g2d.translate(format.getImageableX(), format.getImageableY());
 126         doPaint(g2d);
 127         return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
 128     }
 129 
 130     static final float DIM = 100;
 131     public void doPaint(Graphics2D g2d) {
 132 
 133         g2d.translate(DIM*0.2, DIM*0.2);
 134         Shape s = new Rectangle2D.Float(0, 0, DIM*2, DIM*2);
 135         
 136         // RadialGradientPaint
 137         Point2D centre = new Point2D.Float(DIM/2.0f, DIM/2.0f);
 138         float radius = DIM/2.0f;
 139         Point2D focus = new Point2D.Float(DIM/3.0f, DIM/3.0f);
 140         float stops[] = {0.0f, 1.0f};
 141         Color colors[] =  { Color.red, Color.white} ;
 142 
 143         RadialGradientPaint rgp =
 144            new RadialGradientPaint(centre, radius, focus, stops, colors,
 145               RadialGradientPaint.CycleMethod.NO_CYCLE);
 146         g2d.setPaint(rgp);
 147         g2d.fill(s);    
 148                      
 149         g2d.translate(DIM*2.2, 0);
 150         Color colors1[] =  { Color.red, Color.blue, Color.green} ;
 151         float stops1[] = {0.0f, 0.5f, 1.0f};
 152         RadialGradientPaint rgp1 =
 153            new RadialGradientPaint(centre, radius, focus, stops1, colors1,
 154               RadialGradientPaint.CycleMethod.REFLECT);
 155         g2d.setPaint(rgp1);
 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 stops2[] = {0.0f, 0.3f, 0.6f, 1.0f};
 161         RadialGradientPaint rgp2 =
 162            new RadialGradientPaint(centre, radius, focus, stops2, colors2,
 163               RadialGradientPaint.CycleMethod.REPEAT);
 164         g2d.setPaint(rgp2);
 165         g2d.fill(s);
 166 
 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 RadialGradientPaint graphics will be shown on console.\n"
 184                 + " The same graphics is sent to printer.\n"
 185                 + " Please verify if RadialGradientPaint 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 }