1 /*
   2  * Copyright (c) 2007, 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 /*
  25  * @test
  26  * @key headful
  27  * @bug 6659345
  28  * @summary Tests that various paints work correctly when preceeded by a
  29  * textured operaiton.
  30  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  31  * @run main/othervm AccelPaintsTest
  32  * @run main/othervm -Dsun.java2d.opengl=True AccelPaintsTest
  33  */
  34 
  35 import java.awt.Color;
  36 import java.awt.Dimension;
  37 import java.awt.EventQueue;
  38 import java.awt.GradientPaint;
  39 import java.awt.Graphics;
  40 import java.awt.Graphics2D;
  41 import java.awt.GraphicsConfiguration;
  42 import java.awt.GraphicsEnvironment;
  43 import java.awt.LinearGradientPaint;
  44 import java.awt.MultipleGradientPaint.CycleMethod;
  45 import java.awt.Paint;
  46 import java.awt.RadialGradientPaint;
  47 import java.awt.Rectangle;
  48 import java.awt.Shape;
  49 import java.awt.TexturePaint;
  50 import java.awt.Transparency;
  51 import java.awt.geom.Rectangle2D;
  52 import java.awt.image.BufferedImage;
  53 import java.awt.image.VolatileImage;
  54 import java.io.File;
  55 import java.io.IOException;
  56 import java.lang.reflect.InvocationTargetException;
  57 import javax.imageio.ImageIO;
  58 import javax.swing.JFrame;
  59 import javax.swing.JPanel;
  60 
  61 public class AccelPaintsTest extends JPanel {
  62     BufferedImage bi =
  63         new BufferedImage(80, 100, BufferedImage.TYPE_INT_ARGB_PRE);
  64 
  65     RadialGradientPaint rgp =
  66         new RadialGradientPaint(100, 100, 100, new float[] {0f, 0.2f, 0.6f, 1f},
  67             new Color[] { Color.red,
  68                           Color.yellow,
  69                           Color.blue,
  70                           Color.green},
  71             CycleMethod.REFLECT);
  72     LinearGradientPaint lgp =
  73             new LinearGradientPaint(30, 30, 120, 130, new float[] {0f, 0.2f, 0.6f, 1f},
  74             new Color[] {Color.red,
  75                          Color.yellow,
  76                          Color.blue,
  77                          Color.green});
  78     GradientPaint gp =
  79             new GradientPaint(30, 30, Color.red, 120, 130, Color.yellow, true);
  80 
  81     TexturePaint tp =
  82             new TexturePaint(bi, new Rectangle2D.Float(30, 30, 120, 130));
  83 
  84 
  85     public AccelPaintsTest() {
  86         Graphics g = bi.getGraphics();
  87         g.setColor(Color.blue);
  88         g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
  89 
  90         setPreferredSize(new Dimension(250, 4*120));
  91     }
  92 
  93     private void renderWithPaint(Graphics2D g2d, Paint p) {
  94         g2d.drawImage(bi, 130, 30, null);
  95 
  96         g2d.setPaint(p);
  97         g2d.fillRect(30, 30, 80, 100);
  98     }
  99 
 100     private void render(Graphics2D g2d) {
 101         renderWithPaint(g2d, rgp);
 102         g2d.translate(0, 100);
 103 
 104         renderWithPaint(g2d, lgp);
 105         g2d.translate(0, 100);
 106 
 107         renderWithPaint(g2d, gp);
 108         g2d.translate(0, 100);
 109 
 110         renderWithPaint(g2d, tp);
 111         g2d.translate(0, 100);
 112     }
 113 
 114     private void test() {
 115         GraphicsConfiguration gc =
 116             GraphicsEnvironment.getLocalGraphicsEnvironment().
 117                 getDefaultScreenDevice().getDefaultConfiguration();
 118         if (gc.getColorModel().getPixelSize() < 16) {
 119             System.out.println("<16 bit depth detected, test passed");
 120             return;
 121         }
 122 
 123         VolatileImage vi =
 124             gc.createCompatibleVolatileImage(250, 4*120, Transparency.OPAQUE);
 125         BufferedImage res;
 126         do {
 127             vi.validate(gc);
 128             Graphics2D g2d = vi.createGraphics();
 129             g2d.setColor(Color.white);
 130             g2d.fillRect(0, 0, vi.getWidth(), vi.getHeight());
 131 
 132             render(g2d);
 133 
 134             res = vi.getSnapshot();
 135         } while (vi.contentsLost());
 136 
 137         for (int y = 0; y < bi.getHeight(); y++) {
 138             for (int x = 0; x < bi.getWidth(); x++) {
 139                 if (res.getRGB(x, y) == Color.black.getRGB()) {
 140                     System.err.printf("Test FAILED: found black at %d,%d\n",
 141                                       x, y);
 142                     try {
 143                         String fileName = "AccelPaintsTest.png";
 144                         ImageIO.write(res, "png", new File(fileName));
 145                         System.err.println("Dumped rendering to " + fileName);
 146                     } catch (IOException e) {}
 147                     throw new RuntimeException("Test FAILED: found black");
 148                 }
 149             }
 150         }
 151     }
 152 
 153     protected void paintComponent(Graphics g) {
 154         super.paintComponent(g);
 155         Graphics2D g2d = (Graphics2D)g;
 156 
 157         render(g2d);
 158     }
 159 
 160     public static void main(String[] args)
 161         throws InterruptedException, InvocationTargetException
 162     {
 163 
 164         if (args.length > 0 && args[0].equals("-show")) {
 165             EventQueue.invokeAndWait(new Runnable() {
 166                 public void run() {
 167                     JFrame f = new JFrame("RadialGradientTest");
 168                     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 169                     AccelPaintsTest t = new AccelPaintsTest();
 170                     f.add(t);
 171                     f.pack();
 172                     f.setVisible(true);
 173                 }
 174             });
 175         } else {
 176             AccelPaintsTest t = new AccelPaintsTest();
 177             t.test();
 178             System.out.println("Test Passed.");
 179         }
 180     }
 181 }