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